2014-11-04 101 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