note 53525 modified in function.mysql-list-fields by danbrown

From: Date: Tue, 05 May 2009 13:37:57 +0000
Subject: note 53525 modified in function.mysql-list-fields by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-154232@lists.php.net to get a copy of this message
To make an array containing the names of a column from a table, you use the following simple script: (Note: using table1 with three columns) <?php $qColumnNames = mysql_query("SHOW COLUMNS FROM table1",$db) or die("mysql error"); $numColumns = mysql_num_rows($qColumnNames); $x = 0; while ($x < $numColumns) { $colname = mysql_fetch_row($qColumnNames); $col[$colname[0]] = $colname[0]; $x++; } print_r($col); /* it will have the following output Array ( [firstcolumn] => firstcolumn [secondcolumn] => secondcolumn [thirdcolumn] => thirdcolumn ) */ ?> Or you can number the columns from zero. This help when using it in conjunction with mysql_fetch_row to get an array. You don't have to remember which number of the array a certain column is. <?php $qColumnNames = mysql_query("SHOW COLUMNS FROM table1",$db) or die("mysql error"); $numColumns = mysql_num_rows($qColumnNames); $x = 0; while ($x < $numColumns) { $colname = mysql_fetch_row($qColumnNames); $col[$colname[0]] = $x; $x++; } print_r($col); /* it will have the following output Array ( [firstcolumn] => 0 [secondcolumn] => 1 [thirdcolumn] => 2 ) */ ?> To use it in conjuction with mysql_fetch_row: <?php $row = mysql_fetch_row("SELECT * from table1",$db) or die("mysql error"); ?> You can now call $row[col[firstcolumn]]. This becomes useful when you have a lot of columns. --was-- To make an array containing the names of a column from a table, you use the following simple script: (Note: using table1 with three columns) $qColumnNames = mysql_query("SHOW COLUMNS FROM table1",$db) or die("mysql error"); $numColumns = mysql_num_rows($qColumnNames); $x = 0; while ($x < $numColumns) { $colname = mysql_fetch_row($qColumnNames); $col[$colname[0]] = $colname[0]; $x++; } print_r($col); /* it will have the following output Array ( [firstcolumn] => firstcolumn [secondcolumn] => secondcolumn [thirdcolumn] => thirdcolumn ) */ Or you can number the columns from zero. This help when using it in conjunction with mysql_fetch_row to get an array. You don't have to remember which number of the array a certain column is. $qColumnNames = mysql_query("SHOW COLUMNS FROM table1",$db) or die("mysql error"); $numColumns = mysql_num_rows($qColumnNames); $x = 0; while ($x < $numColumns) { $colname = mysql_fetch_row($qColumnNames); $col[$colname[0]] = $x; $x++; } print_r($col); /* it will have the following output Array ( [firstcolumn] => 0 [secondcolumn] => 1 [thirdcolumn] => 2 ) */ To use it in conjuction with mysql_fetch_row: $row = mysql_fetch_row("SELECT * from table1",$db) or die("mysql error"); You can now call $row[col[firstcolumn]]. This becomes useful when you have a lot of columns. http://php.net/manual/en/function.mysql-list-fields.php

« previous php.notes (#154232) next »