我将从select语句中获得输出 - > json_encode,但不起作用。我的屏幕是空白的,请帮帮我。 我使用xammp for Mac。
<?php
$servername = "localhost";
$username = "root";
$password = "";
try {
$conn = new PDO("mysql:host=$servername;dbname=app", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}
$stmt = $conn->prepare("SELECT Name,Adresse,Latitude,Longi from Location");
$stmt->execute();
$row = $stmt->fetchAll(PDO::FETCH_ASSOC);
//var_dump($row);
// If so, then create a results array and a temporary one
// to hold the data
$resultArray = array();
$tempArray = array();
// Add each row into our results array
$tempArray = $row;
array_push($resultArray, $tempArray);
// Finally, encode the array to JSON and output the results
echo json_encode($tempArray,true);
$conn = null;
?>
答案 0 :(得分:1)
您的代码段对我来说很好。
您可以使用print_r
或类似的东西进行尝试,或检查您的数据库是否包含特殊字符