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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Draco_03
Forum Regular
Posts: 577
Joined: Fri Aug 15, 2003 12:25 pm
Location: Montreal, Canada

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

Post 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
Last edited by Draco_03 on Thu Jul 22, 2004 10:58 am, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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".
User avatar
liljester
Forum Contributor
Posts: 400
Joined: Tue May 20, 2003 4:49 pm

Post 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> 
   "); 
} 
?>
Draco_03
Forum Regular
Posts: 577
Joined: Fri Aug 15, 2003 12:25 pm
Location: Montreal, Canada

Post by Draco_03 »

Thank you very much
I understood.
Post Reply