I could not retrieve information from mysql database. I got an error saying
Array\n(\n [0] => 42000\n [1] => 1064\n [2] => on the client side
However, when I replace the variable $city with regular string like "san francisco" it works fine. I even tested that whether $city is a string and it is a string
$sth just failed to execute even though the query was successful
<?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();
}