...

View Full Version : newbie simple php query!



obaluba
02-01-2007, 12:57 AM
Hi All,

Thanks for looking, I have, what I hope is a simple query, I'm a newbie to PHP and am trying to get some records to display from a mysql database done in phpmyadmin... Now the php seems to run fine, but only displays 1 row from the database which is 'Web' and it does so in the wrong place (where First name & lastname should be)


<?
$user='cdesi_admin';
$password='xxxxxx';
$database='cdesi_contacts';

$dbh=mysql_connect ("localhost", "cdesi_admin", "xxxxxx") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("carrdesi_contacts");

$query="SELECT * FROM contacts";
$result=mysql_query($query);
$num=mysql_numrows($result);

mysql_close();

echo "<b><center>Database Output</center></b><br><br>";

$i=0;
while ($i < $num){

$first=mysql_result($result,$i,"first");
$first=mysql_result($result,$i,"last");
$first=mysql_result($result,$i,"phone");
$first=mysql_result($result,$i,"mobille");
$first=mysql_result($result,$i,"fax");
$first=mysql_result($result,$i,"email");
$first=mysql_result($result,$i,"web");

echo"<b>Name: $first $last</b><br>Phone: $phone<br>Mobile: $mobile<br>Fax: $fax<br>E-mail: $email<br>Web: $web<br><hr><br>";

$i++;
}
?>

this has had me stumped, so any advice would be very much appreciated!

thanks for looking!

:)

CFMaBiSmAd
02-01-2007, 01:15 AM
If you proof-read your code, you will notice that you are assigning everything to the same variable - $first.

The first step in debugging code that does not produce the expected results is to proof-read it to make sure that it contains the logic that was intended. This is true irregardless of the experience level of the programmer that wrote it.

Mhtml
02-01-2007, 04:00 AM
Yeah, what CFMaBiSmAd (what is the significance of your name BTW?:confused: ) said...


$first=mysql_result($result,$i,"first");
$first=mysql_result($result,$i,"last");//<-- should be $last =
$first=mysql_result($result,$i,"phone");//<-- should be $phone =
$first=mysql_result($result,$i,"mobille");//<-- should be $mobile =
$first=mysql_result($result,$i,"fax");//<-- should be $fax =
$first=mysql_result($result,$i,"email");//<-- should be $email =
$first=mysql_result($result,$i,"web");//<-- should be $web =

obaluba
02-01-2007, 08:23 AM
thankyou for your direction, stupid mistake! working fine now!

thanks again :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum