0

I really hope I'm not just being stupid. I'm trying to retrieve two values from a existing database, based on a user's ID. I've been trying a couple of different things, so I've got some commented code in case I was close!

I'm using a select statement to get the values, and then trying to pass them to PHP variables. I'm getting the values from the DB, just not setting them as PHP variables.

<?php
//declare database variables
header('Content-type: text/html; charset=utf-8');
 $username = "ishuttle";
$password = "";
$hostname = "localhost"; 
$dbname = "ishuttle_taxi-location";

$conn = mysql_connect($hostname, $username, $password);
if(! $conn )
{
  die('Could not connect: ' . mysql_error());
}



//search database for driver location
 
 mysql_select_db('ishuttle_taxi-location');
 
 $sql = "SELECT _id, Latitude, Longitude
FROM driver_location
WHERE _id = 2";




$retval = mysql_query( $sql, $conn );
if(! $retval )
{
  die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_assoc($retval))
{
    echo "Lat :{$row['Latitude']}  <br> ".
         "Long: {$row['Longitude']} <br> ".
         "--------------------------------<br>";
} 
echo "Fetched data successfully\n";



$Lat = $retval[1];
$Long = $retval[2];
echo "$Lat";



?>

Cheers in advance :)

2 Answers2

1

Assign the variables inside the while loop. Change

while($row = mysql_fetch_assoc($retval))
{
    echo "Lat :{$row['Latitude']}  <br> ".
         "Long: {$row['Longitude']} <br> ".
         "--------------------------------<br>";
} 

to

while($row = mysql_fetch_assoc($retval))
{
         $Lat = $row['Latitude'];
         $Long = $row['Longitude'];
         echo "Lat :{$row['Latitude']}  <br> ".
         "Long: {$row['Longitude']} <br> ".
         "--------------------------------<br>";
} 
fislerdata
  • 290
  • 1
  • 4
  • 8
0

This is wrong:

$Lat = $retval[1];
        ^^^^^^

$retval is your result handle from the mysql query. It's NOT an array of results.

Marc B
  • 356,200
  • 43
  • 426
  • 500