Forum Moderators: coopster

Message Too Old, No Replies

Showing values from database

         

oli_collins

11:38 am on Feb 19, 2010 (gmt 0)

10+ Year Member



Im trying to pull some data from two tables in a database. There are 116 results but it only shows 5. Below is an example of the code i am using:

$sql_str="SELECT * FROM `regional_managers` JOIN `postcode_data` ON postcode_data.rm_id=regional_managers.rm_id ";
$sql = mysql_query($sql_str);
$result = mysql_fetch_assoc($sql);
$num_rows = mysql_num_rows($sql);
echo "There are ".$num_rows." results.<br />";
if(!$sql){
die('Could not query database: ' . mysql_error());
}
print_r($result);?>
<br />
<? foreach ($result as $value){ ?>
Name: <? echo $result['manager_name'];?> Email: <? echo $result['manager_email'];?><br />
<? } ?>


If i use $value'manager_name'] for example it just shows 1 or C rather then a full value.
The printed array shows as
Array ( [rm_id] => 1 [manager_name] => Bob Tester [manager_email] => bob@test.co.uk [pc_id] => 1 [post_code_start] => SA ) 


I have taken data from a database load of time but am not sure what am i doing wrong. Can anyone spot anything?

Matthew1980

12:07 pm on Feb 19, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there oli_collins,

Welcome to the forum,

Firstly, when you want to iterate through a databse array, use a while loop, and not a for each, and its always preferable to use the full tags when opening a php file ie: <?php


<?php
$sql_str="SELECT * FROM `regional_managers` JOIN `postcode_data` ON postcode_data.rm_id=regional_managers.rm_id ";
$sql = mysql_query($sql_str) or die(mysql_error());

if($sql){//open if
$num_rows = mysql_num_rows($sql);
echo "There are ".$num_rows." results.<br />";

while($result = mysql_fetch_array($sql))
{//open while
?>
Name: <?php echo $result['manager_name'];?> Email: <?php echo $result['manager_email'];?><br />
<?php
}//close while
}else{
echo "No results returned";
exit;
}//close else/if



As before you were asking for $result['manager_name']; where you needed $value['manager_name'];

Hope as that points you in the right direction ;-p

Cheers,

MRb