Populate a PHP Dropdown List from MySQL Database -
i'm trying populate dropdown list in web page mysql database table has 1 column (pathology_id). know there test data in there best can populate box field name, not row values. code have far below, can suggest how more column name? in advance.
<?php $con = mysql_connect("localhost","dname","dbpass"); if(!$con) { die('could not connect: ' . mysql_error()); } $fields = mysql_list_fields("dbname","pathology",$con); $columns = mysql_num_fields($fields); echo "<form action = newcase.php method = post><select name = field>"; for($i = 0; $i < $columns ; $i++) { echo "<option value = $i>"; echo mysql_field_name($columns , $i); } echo "</select></form>"; if(!mysql_query($sql,$con)) { die('error: ' . mysql_error()); } else { echo "1 record added"; } mysql_close($con) ?>
try this:
<?php // supplied user, example $firstname = 'fred'; $lastname = 'fox'; // formulate query // best way perform sql query // more examples, see mysql_real_escape_string() $query = sprintf("select firstname, lastname, address, age friends firstname='%s' , lastname='%s'", mysql_real_escape_string($firstname), mysql_real_escape_string($lastname)); // perform query $result = mysql_query($query); // check result // shows actual query sent mysql, , error. useful debugging. if (!$result) { $message = 'invalid query: ' . mysql_error() . "\n"; $message .= 'whole query: ' . $query; die($message); } // use result // attempting print $result won't allow access information in resource // 1 of mysql result functions must used // see mysql_result(), mysql_fetch_array(), mysql_fetch_row(), etc. while ($row = mysql_fetch_assoc($result)) { echo $row['firstname']; echo $row['lastname']; echo $row['address']; echo $row['age']; } // free resources associated result set // done automatically @ end of script mysql_free_result($result); ?>
from php: mysql_query().
mysql_list_fields
returns information given table, not data contained.
Comments
Post a Comment