我无法从mysql数据库中检索信息。我收到了一个错误
数组\ n(\ n [0] => 42000 \ n [1] => 1064 \ n [2] =>在客户端
然而,当我用常规字符串替换变量 $ city 时,像#34;旧金山"它工作正常。我甚至测试过 $ city 是否是一个字符串,它是一个字符串
$ sth 即使查询成功也无法执行
<?php
if (isset($_GET)) {
$servername = "localhost";
$username = "XXXXX";
$password = "XXXXX";
$dbname = "CALIFORNIA";
$city = $_GET['userinput'] . "";
// Create connection
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
$sql = "SELECT State FROM CITY WHERE City_name = $city"; // When I replace the $city with string like "San Francisco", it works but not with variable $city
if (($sth = $conn->prepare($sql))) {
if ($sth->execute()) { // $sth failed even though the query was successful
$result = $sth->fetchAll();
$json = json_encode($result);
echo $json;
}
else
echo print_r($sth->errorInfo()); // Error print out on client side
}
else
echo $conn->errorCode();
}