2016-07-22 5 views
0

Ich hoffe wirklich, ich bin nicht nur dumm. Ich versuche anhand der Benutzer-ID zwei Werte aus einer vorhandenen Datenbank abzurufen. Ich habe ein paar verschiedene Dinge versucht, also habe ich einen kommentierten Code für den Fall, dass ich in der Nähe bin!Kann nicht MySQL Tabelleneintrag in PHP Variable

Ich verwende eine SELECT-Anweisung, um die Werte zu erhalten, und versuche dann, sie an PHP-Variablen zu übergeben. Ich bekomme die Werte von der DB, nur nicht als PHP-Variablen.

<?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"; 
 

 

 

 
?>

Prost im Voraus :)

+0

Überprüfen Sie unsere mysqli_ oder PDO. Die msql_ für alle Intents tot in neueren Version von PHP. – nerdlyist

Antwort

1

die Variablen innerhalb der Schleife while zuordnen. Ändern

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

zu

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

Sie majestätischen SOB. Ich danke dir sehr. Das hat mah Speck gerettet. – samgarbett

+1

Und dann lesen Sie, wie Sie SQL-Injektion http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php/60496#60496 verhindern – fislerdata

0

Das ist falsch:

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

$ retval ist Ihr Ergebnis Griff aus der MySQL-Abfrage. Es ist keine Reihe von Ergebnissen.