07-22-2003, 12:40 AM | #1 (permalink) |
Insane
|
PHP and MySQL question
If you want to get all possible values for a SET column, you should use: SHOW COLUMNS FROM table_name LIKE set_column_name and parse the SET definition in the second column.
Ok, that's nice, but what the hell does it mean? Say I have a SET column in my database, with 10 values. I want to create an HTML select item, with the values being the values from the possible values in the SET column. I found how to do this from items already entered. I found (sorta) how to get the possible values...but I don't know how to access them... Any ideas? MPEDrummer
__________________
My sig can beat up your honor student. |
07-22-2003, 07:04 AM | #2 (permalink) |
Upright
|
If it is just the SQL that you are talking about, then the statement is a SELECT statement.
The output that you need will vary wildly on the data that you need to extract from the table(s). The basic format would be "Select [fields] from [table(s)] where [criteria];" A better guide can be found here: http://www.devhood.com/tutorials/tut...tutorial_id=74 The best way to extract/display data is to use SQL for the extraction and then PHP/HTML/Whatever to display the data. That way you don't have to worry about creating some rocket science code to parse data that you have taken from the db to display. Just my $0.02... Let me know if this helps. Thx, Scyther |
07-22-2003, 09:46 AM | #4 (permalink) |
Insane
|
I sorta got it, but I have another question, this time with examples.
Code:
function get_set($table,$column) { $sql = "SHOW COLUMNS FROM $table LIKE '$column'"; if (!($ret = mysql_query($sql))){ die("Error: Could not show columns"); } $line = mysql_fetch_assoc($ret); $set = $line['Type']; $set = substr($set,5,strlen($set)-7); // Remove"set(" at start and ");" at end return preg_split("/','/",$set); // Split into and array } http://test.komta.com/test%20form.php You can see it in action there. All the select items were created this way, using the following call... Code:
// Create colorType select field $colorset = $db->get_set(iris, colorType); echo 'select name="colorType" size="1"\n'; foreach ($colorset as $row) { echo "option value =\"$row\">$row</option>\n"; } echo '</select><br>'; // End colorType /select // I had to get rid of the < before select and option, TFP was trying to render it So, here's my question...if you look at the selects, you'll notice that 3 of them have an ' in front of the first value. I don't know where it's coming from, and I'd really like to get rid of it... I don't think it's the function, because it works with the other ones with no problem. Shit. I know what it is...the ones that aren't working are ENUM, not SET, so when I remove the first 5 characters of $set, I'm not removing enough...ok, nevermind. I'm posting this anyway because it was a pain in the ass to type. MPEDrummer
__________________
My sig can beat up your honor student. Last edited by mpedrummer2; 07-22-2003 at 09:58 AM.. |
Tags |
mysql, php, question |
|
|