Page 1 of 1

[SOLVED]Help : Displaying partial or wrong info from DB

Posted: Thu Jul 22, 2004 10:25 am
by Draco_03

Code: Select all

<?php
$sql = "SELECT * FROM edh_provinces";
$result = mysql_query($sql);

for ($i=0, $rows=mysql_num_rows($result); $i<$rows; $i++){
	$prov = mysql_fetch_array($result);
	echo ("
		<option value="$prov[$i]">$prov[$i]</option>
	");
}
?>
Yep.. only taking first row outta my provinces table.

so i tried That

Code: Select all

<?php
$sql = "SELECT * FROM edh_provinces";
$result = mysql_query($sql);

for ($i=0, $rows=mysql_num_rows($result); $i<$rows; $i++){
	$prov[$i] = mysql_fetch_array($result);
	echo ("
		<option value="$prov[$i]">$prov[$i]</option>
	");
}
?>
But now display all my field as Array

Ty for your help

Posted: Thu Jul 22, 2004 10:41 am
by feyd
You need to tell the engine which field(s) you wish to print.. Right now, you are asking it to print the array, which in this manor will always do an array to string conversion, hence just printing "Array".

Posted: Thu Jul 22, 2004 10:42 am
by liljester
your logic is wrong. the loop is fine, but when you print your array you wouldnt want to use $i, you need to use the db field names because mysql_fetch_array returns an array of each row. the array is populated with a numeric keys as well as named keys with the same name of your db fields.

so if your query was like this : "SELECT province_id, province_name FROM ebh_provinces;"
$row = mysql_fetch_array would return the following array
$row[0] = "1"
$row[1] = "Alberta"
$row[province_id] = "1"
$row[province_name] = "Alberta"

Code: Select all

$sql = "SELECT * FROM edh_provinces"; 
$result = mysql_query($sql); 

for ($i=0, $rows=mysql_num_rows($result); $i<$rows; $i++){ 
   $prov = mysql_fetch_array($result); 
   echo (" 
      <option value="$prov[province_id]">$prov[province_name]</option> 
   "); 
} 
?>

Posted: Thu Jul 22, 2004 10:49 am
by Draco_03
Thank you very much
I understood.