2014-11-04 66 views
1

我正在研究需要從MySQL數據庫中選擇數據的應用程序。我目前正在通過瀏覽器測試PHP腳本,以確保它正在返回正確的數據。這個問題目前它返回「數據庫錯誤!」異常。我已經包含了我的PHP腳本。PHP MySQL選擇腳本

get_agencies_by_city.php

<?php 

/* 
* Following code will get all agencies matching the query 
* Returns essential details 
* An agency is identified by agency id 
*/ 

require("DB_Link.php"); 

$city = ($_GET['City']); 

//query database for matching agency 
$query = "SELECT * FROM agency WHERE City = $city"; 

//Execute query 
try { 
    $stmt = $db->prepare($query); 
    $result = $stmt->execute(); 
} 
catch (PDOException $ex) { 
    $response["success"] = 0; 
    $response["message"] = "Database Error!"; 
    die(json_encode($response)); 
} 

//Retrieve all found rows and add to array 
$rows = $stmt->FETCHALL(); 


if($rows) { 
    $response["success"] = 1; 
    $response["message"] = "Results Available!"; 
    $response["agencys"] = array(); 

    foreach ($rows as $row) { 
     $agency   = array(); 
     $agency["AgencyID"] = $row["AgencyID"]; 
     $agency["AgencyName"] = $row["AgencyName"]; 
     $agency["Address1"] = $row["Address1"]; 
     $agency["City"]  = $row["City"]; 
     $agency["State"] = $row["State"]; 
     $agency["Zip"]  = $row["Zip"]; 
     $agency["Lat"]  = $row["Lat"]; 
     $agency["Lon"]  = $row["Lon"]; 

     //update response JSON data 
     array_push($response["agencys"], $agency); 
    } 

    //Echo JSON response 
    echo json_encode($response); 

} else { 
    $response["success"] = 0; 
    $response["message"] = "No Agency found!"; 
    die(json_encode($response)); 
} 

?> 

這裏是DB_Link.php

<?php 

// These variables define the connection information the MySQL database 
// set connection... 


$options = array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'); 


try 
{ 

     $db = new PDO("mysql:host={$host};dbname={$dbname};charset=utf8", $username, $password, $options); 
} 
catch(PDOException $ex) 
{ 

     die("Failed to connect to the database: " . $ex->getMessage()); 
} 


$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 


$db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); 


if(function_exists('get_magic_quotes_gpc') && get_magic_quotes_gpc()) 
{ 
     function undo_magic_quotes_gpc(&$array) 
     { 
      foreach($array as &$value) 
      { 
       if(is_array($value)) 
       { 
        undo_magic_quotes_gpc($value); 
       } 
       else 
       { 
        $value = stripslashes($value); 
       } 
      } 
     } 

     undo_magic_quotes_gpc($_POST); 
     undo_magic_quotes_gpc($_GET); 
     undo_magic_quotes_gpc($_COOKIE); 
} 


header('Content-Type: text/html; charset=utf-8'); 


session_start(); 


?> 
+0

給出DB_Link.php代碼也 – 2014-11-04 23:18:36

+1

你試過看PDO異常的內容?這可以告訴你有什麼問題 – AlexL 2014-11-04 23:18:38

+0

如果你可以直接在服務器上運行SQL語句(比如用mysql命令行程序),這將使它更容易調試並查看錯誤消息。 – 2014-11-04 23:20:08

回答

2

你應該重寫查詢到這一點,因爲它是一個準備好的聲明和您的查詢會更加安全(和工作)!

//your code 

try { 
    $statement = $dbh->prepare("SELECT * FROM agency WHERE city = :city"); 
    $statement->execute(array('city' => $city)); 

    // rest of your code 
} 

    // and the exception 

catch (PDOException $ex) { 

     //or include your error statement - but echo $ex->getMessage() 
     die('Error!: ' . json_encode($ex->getMessage())); 

} 

你也應該檢查$ _GET是否真的設置!

像這樣:

try { 
     $stmt = $dbh->prepare("SELECT * FROM agency WHERE city = :city"); 
     $stmt->execute(array('city' => $city)); 
     $rows = $stmt->FETCHALL(); 


if($rows) { 
    $response["success"] = 1; 
    $response["message"] = "Results Available!"; 
    $response["agencys"] = array(); 

    foreach ($rows as $row) { 
     $agency   = array(); 
     $agency["AgencyID"] = $row["AgencyID"]; 
     $agency["AgencyName"] = $row["AgencyName"]; 
     $agency["Address1"] = $row["Address1"]; 
     $agency["City"]  = $row["City"]; 
     $agency["State"] = $row["State"]; 
     $agency["Zip"]  = $row["Zip"]; 
     $agency["Lat"]  = $row["Lat"]; 
     $agency["Lon"]  = $row["Lon"]; 

     //update response JSON data 
     array_push($response["agencys"], $agency); 
    } 

    //Echo JSON response 
    echo json_encode($response); 

} } 

catch (PDOException $ex) { 

      //or include your error statement - but echo $ex->getMessage() 
      die('Error!: ' . json_encode($ex->getMessage())); 

    } 
+0

作出了建議的更改,現在我得到「調用一個非對象的成員函數prepare()」在第20行 – IrishCarBomb 2014-11-05 00:01:27

+0

更改$ dbh-> prepare到$ db-> prepare,對不起,我不知道變量您正在使用 – baao 2014-11-05 00:03:59

+0

並刪除您的問題從您的問題!!!!!! – baao 2014-11-05 00:04:33

0

變量$城市需要在您的查詢。做這樣的事情:

$query = "SELECT * FROM Agency WHERE City = " . $city; 
+0

OP的版本與此相同。存在於雙引號字符串中的變量名將在使用字符串時替換其值。 – 2014-11-04 23:27:53

+0

呼叫良好。我正在考慮使用單引號時會發生什麼。 – JMc 2014-11-04 23:31:15