Re: [PHP-DB] Extracting column names from a db
The problem is that you're fetching a single row and then print out the result, whereas Mysql returns a row for each field. Try something like this: mysql_select_db("filterseveuk") or die(mysql_error()); $query = "SHOW COLUMNS FROM " .$table. ""; $result = mysql_query ( $query ) or die( mysql_error () ); while ($row = mysql_fetch_assoc ($result)) echo $row['Field']; This loops through each row in the result set and prints out the value of the field called "Field", which actually contains the name of the field in the table. Cheers, Marco On Sun, 2002-11-10 at 10:18, David Rice wrote: > > > I need to write a script that will extract the names of the columns in my > database eg. "user_id, username" can anyone help as to how to do this! > > I have tried > > ** > *** > mysql_select_db("filterseveuk") or die(mysql_error()); > > $query = "SHOW COLUMNS FROM " .$table. ""; > > $result = mysql_query ( $query ) or die( mysql_error () ); > > $numrows = mysql_num_rows ($result); > > $row = mysql_fetch_array ($result); > > for($x=0; $x <= $numrows; $x++){ > > echo $row[$x] ; > > } > ** > *** > this doesn't work the way i want it to and gives me the output > > ** > *** > user_idint(11)PRIauto_increment > Warning: Undefined offset: 6 in > /home/filterseveuk/public_html/admin/index.php on line 30 > > Warning: Undefined offset: 7 in > /home/filterseveuk/public_html/admin/index.php on line 30 > > Warning: Undefined offset: 8 in > /home/filterseveuk/public_html/admin/index.php on line 30 > > Warning: Undefined offset: 9 in > /home/filterseveuk/public_html/admin/index.php on line 30 > > Warning: Undefined offset: 10 in > /home/filterseveuk/public_html/admin/index.php on line 30 > > Warning: Undefined offset: 11 in > /home/filterseveuk/public_html/admin/index.php on line 30 > > Warning: Undefined offset: 12 in > /home/filterseveuk/public_html/admin/index.php on line 30 > > Warning: Undefined offset: 13 in > /home/filterseveuk/public_html/admin/index.php on line 30 > > Warning: Undefined offset: 14 in > /home/filterseveuk/public_html/admin/index.php on line 30 > > > > > > > > > _ > Tired of spam? Get advanced junk mail protection with MSN 8. > http://join.msn.com/?page=features/junkmail > > > -- > PHP Database Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DB] Extracting column names from a db
A mix-up in your loop. Try: while ( $row = mysql_fetch_array( $result ) ) { echo $row['Field'] ; } (you may just as well use the field names provided by SHOW COLUMNS - more readable than numerical indexes) HTH Ignatius - Original Message - From: "David Rice" <[EMAIL PROTECTED]> To: <[EMAIL PROTECTED]> Sent: Sunday, November 10, 2002 4:18 PM Subject: [PHP-DB] Extracting column names from a db > > > I need to write a script that will extract the names of the columns in my > database eg. "user_id, username" can anyone help as to how to do this! > > I have tried > > ** > *** > mysql_select_db("filterseveuk") or die(mysql_error()); > > $query = "SHOW COLUMNS FROM " .$table. ""; > > $result = mysql_query ( $query ) or die( mysql_error () ); > > $numrows = mysql_num_rows ($result); > > $row = mysql_fetch_array ($result); > > for($x=0; $x <= $numrows; $x++){ > > echo $row[$x] ; > > } > ** > *** > this doesn't work the way i want it to and gives me the output > > ** > *** > user_idint(11)PRIauto_increment > Warning: Undefined offset: 6 in > /home/filterseveuk/public_html/admin/index.php on line 30 > > Warning: Undefined offset: 7 in > /home/filterseveuk/public_html/admin/index.php on line 30 > > Warning: Undefined offset: 8 in > /home/filterseveuk/public_html/admin/index.php on line 30 > > Warning: Undefined offset: 9 in > /home/filterseveuk/public_html/admin/index.php on line 30 > > Warning: Undefined offset: 10 in > /home/filterseveuk/public_html/admin/index.php on line 30 > > Warning: Undefined offset: 11 in > /home/filterseveuk/public_html/admin/index.php on line 30 > > Warning: Undefined offset: 12 in > /home/filterseveuk/public_html/admin/index.php on line 30 > > Warning: Undefined offset: 13 in > /home/filterseveuk/public_html/admin/index.php on line 30 > > Warning: Undefined offset: 14 in > /home/filterseveuk/public_html/admin/index.php on line 30 > > > > > > > > > _ > Tired of spam? Get advanced junk mail protection with MSN 8. > http://join.msn.com/?page=features/junkmail > > > -- > PHP Database Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP-DB] Extracting column names from a db
I need to write a script that will extract the names of the columns in my database eg. "user_id, username" can anyone help as to how to do this! I have tried ** *** mysql_select_db("filterseveuk") or die(mysql_error()); $query = "SHOW COLUMNS FROM " .$table. ""; $result = mysql_query ( $query ) or die( mysql_error () ); $numrows = mysql_num_rows ($result); $row = mysql_fetch_array ($result); for($x=0; $x <= $numrows; $x++){ echo $row[$x] ; } ** *** this doesn't work the way i want it to and gives me the output ** *** user_idint(11)PRIauto_increment Warning: Undefined offset: 6 in /home/filterseveuk/public_html/admin/index.php on line 30 Warning: Undefined offset: 7 in /home/filterseveuk/public_html/admin/index.php on line 30 Warning: Undefined offset: 8 in /home/filterseveuk/public_html/admin/index.php on line 30 Warning: Undefined offset: 9 in /home/filterseveuk/public_html/admin/index.php on line 30 Warning: Undefined offset: 10 in /home/filterseveuk/public_html/admin/index.php on line 30 Warning: Undefined offset: 11 in /home/filterseveuk/public_html/admin/index.php on line 30 Warning: Undefined offset: 12 in /home/filterseveuk/public_html/admin/index.php on line 30 Warning: Undefined offset: 13 in /home/filterseveuk/public_html/admin/index.php on line 30 Warning: Undefined offset: 14 in /home/filterseveuk/public_html/admin/index.php on line 30 _ Tired of spam? Get advanced junk mail protection with MSN 8. http://join.msn.com/?page=features/junkmail -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php