[PLUG] [OT] PHP & Mysql Query

Rohan Dighe rsdighe at fedoraproject.org
Sat Aug 4 11:57:49 IST 2007


Well,

Actually i am stuck at a point in php

this is the string that i have:-
set('punjabi','chinese','italian','thai','southindian','gujarati','jain','maharashtrian','pakistani','pizza','dhaba','sindhi','continental')

now i have 2 queries:-

1) How do i remove or cut some characters of the string.. like in the
above case i want to remove " set( " and from the tail of the above
string i want to chop off " ) " . i scrolled throught the string
functions but i am unable to do so..

i even tried replace for " set( " with " space " and then trim but it
did not work !

2) after the above has been done i want to convert it to array,  i
have written the function myself

function csv2array($str) {
    $str = trim($str);
    $str = preg_replace('/(\s*,\s*)+/', ',', $str);
    $array = explode(",", $str);
    return $array;
}

and it works nicely ! is there any automatic way of doing csv to array?

my IDE gives me this function str_getcsv() but it does not work..
It says:-
Fatal error: Call to undefined function str_getcsv()
please suggest some alternative.


Now, as far as Mysql is concerned:-

It's a problem regarding Mysql set datatype:-

 CREATE TABLE set_test(
    rowid INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
    myset SET('Travel','Sports','Dancing','Fine Dining')
  );

  INSERT INTO set_test(myset) VALUES('Travel,Sports');
  INSERT INTO set_test(myset) VALUES('Travel,Sports,Dancing');

Problem:-
what i want to do is select distinct myset entries...like in the above case
i want my output to be

Travel
Sports
Dancing
when i run some select query !

So, you see that "Fine Dining" exists in the original set but i am not
using it so i want to show only the used myset values.


In read the mysql documentation but it does not give a clear
indication. Well, the other thing that i tried and worked was say

SELECT * FROM set_test WHERE myset = 'Travel,Sports';

then it works..  or even this works

SELECT * FROM set_test WHERE myset LIKE '%Travel%Sports%';


i just want to have distinct among my resultset.

Hope i have stated my problem clearly.

Link to mysql set datatype just for convenience.
http://dev.mysql.com/tech-resources/articles/mysql-set-datatype.html


I will be very thankful for any advice.
-- 
Rohan Dighe
Fedora Ambassador India.
http://fedoraproject.org/wiki/RohanDighe


--
You do not really understand something unless you can explain it to
your grandmother.
 -- Albert Einstein




More information about the Plug-mail mailing list