2013-11-04 174 views
0

我已經更新Mysql Db的php頁面。我不明白爲什麼我的下面的PHP代碼是說「無法更新標記,沒有選擇數據庫」。奇怪!!你能告訴我爲什麼它顯示錯誤信息。mysql連接錯誤問題

謝謝。

PHP代碼:

<?php 

// database settings 
$db_username = 'root'; 
$db_password = ''; 
$db_name = 'parkool'; 
$db_host = 'localhost'; 

//mysqli 
$mysqli = new mysqli($db_host, $db_username, $db_password, $db_name); 

if (mysqli_connect_errno()) 
{ 
header('HTTP/1.1 500 Error: Could not connect to db!'); 
exit(); 
} 

################ Save & delete markers ################# 
if($_POST) //run only if there's a post data 
{ 
//make sure request is comming from Ajax 
$xhr = $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest'; 
if (!$xhr){ 
    header('HTTP/1.1 500 Error: Request must come from Ajax!'); 
    exit(); 
} 

// get marker position and split it for database 
$mLatLang = explode(',',$_POST["latlang"]); 
$mLat  = filter_var($mLatLang[0], FILTER_VALIDATE_FLOAT); 
$mLng  = filter_var($mLatLang[1], FILTER_VALIDATE_FLOAT); 




$mName  = filter_var($_POST["name"], FILTER_SANITIZE_STRING); 
$mAddress = filter_var($_POST["address"], FILTER_SANITIZE_STRING); 
$mId  = filter_var($_POST["id"], FILTER_SANITIZE_STRING); 

/*$result = mysql_query("SELECT id FROM test.markers WHERE test.markers.lat=$mLat 
AND test.markers.lng=$mLng"); 
if (!$result) { 
echo 'Could not run query: ' . mysql_error(); 
exit; 
} 
$row = mysql_fetch_row($result); 
$id=$row[0];*/ 
//$output = '<h1 class="marker-heading">'.$mId.'</h1><p>'.$mAddress.'</p>'; 
//exit($output); 
//Update Marker 
if(isset($_POST["update"]) && $_POST["update"]==true) 
{ 

    $results = mysql_query("UPDATE parkings SET latitude = '$mLat', longitude 
= '$mLng' WHERE locId = '94' "); 
    if (!$results) { 
     //header('HTTP/1.1 500 Error: Could not Update Markers! $mId'); 
     echo "coudld not update marker." . mysql_error(); 
     exit(); 
    } 

    exit("Done!"); 
} 

$output = '<h1 class="marker-heading">'.$mName.'</h1><p>'.$mAddress.'</p>'; 
exit($output); 
} 


############### Continue generating Map XML ################# 

//Create a new DOMDocument object 
$dom = new DOMDocument("1.0"); 
$node = $dom->createElement("markers"); //Create new element node 
$parnode = $dom->appendChild($node); //make the node show up 

// Select all the rows in the markers table 
$results = $mysqli->query("SELECT * FROM parkings WHERE 1"); 
if (!$results) {  
header('HTTP/1.1 500 Error: Could not get markers!'); 
exit(); 
} 

//set document header to text/xml 
header("Content-type: text/xml"); 

// Iterate through the rows, adding XML nodes for each 
while($obj = $results->fetch_object()) 
{ 
$node = $dom->createElement("marker"); 
$newnode = $parnode->appendChild($node); 
$newnode->setAttribute("name",$obj->name); 
$newnode->setAttribute("locId",$obj->locId); 
$newnode->setAttribute("address", $obj->address); 
$newnode->setAttribute("latitude", $obj->latitude); 
$newnode->setAttribute("longitude", $obj->longitude); 
//$newnode->setAttribute("type", $obj->type); 
} 

echo $dom->saveXML(); 
+0

它的意思是你的連接無法選擇數據庫。 – Dinesh

+0

是的,但是爲什麼? – Alex

回答

1

你使用MySQL庫混合的mysqli上執行MySQL查詢。你看,在這裏你連接到數據庫,使用的MySQLi庫:

$mysqli = new mysqli($db_host, $db_username, $db_password, $db_name); 

這裏使用的是mysql_ *功能:

$results = mysql_query("UPDATE parkings SET latitude = '$mLat', longitude 
= '$mLng' WHERE locId = '94' "); 
    if (!$results) { 
     //header('HTTP/1.1 500 Error: Could not Update Markers! $mId'); 
     echo "coudld not update marker." . mysql_error(); 
     exit(); 
    } 

當然,你會得到一個錯誤。

請把所有東西都轉換成MySQLi,它應該可以工作。

+0

它的工作。謝謝。 – Alex

0

也許你正嘗試將mysqli的連接

$mysqli = new mysqli($db_host, $db_username, $db_password, $db_name); 

$results = mysql_query("UPDATE parkings SET latitude = '$mLat', longitude = '$mLng' WHERE locId = '94' ");