(JSON file)Wamp中有什么函数可以使它正常工作吗?
(JSON file)Is there any function to action in Wamp to make it work properly?
这是我的 php 文件,用于使用 android 中的 recycleview
检索用户信息。
但它不起作用我不知道。我需要协助
这里,image是TEXT和String
,其他都是varchar String
也在我的数据库里'xxxxx'table---users
<?php
include("connect.php");
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT nom, prenom, telephone, email,ncni, datenaissance, image FROM users ORDER BY id DESC ";
$result = $conn->query($sql);
if ($result->num_rows >0) {
while($row[] = $result->fetch_assoc()) {
$tem = $row;
$json = json_encode($tem);
}
} else {
echo "No Results Found.";
}
echo $json;
$conn->close();
?>
你的代码有很多错误。你跳过了 mysqli (query,fetch_assoc,num_rows should be mysqli_query,mysqli_fetch_assoc,mysqli_num_rows)无处不在。如果你不想多次写mysqli,你应该试试PDO。
这是一个例子:
try {
$db = new PDO('mysql:host=localhost;dbname=DBNAME;charset=utf8',DBUSER, DBPASS);
//echo "Connected";
}
catch (PDOException $e) {
//print "Error!: " . $e->getMessage() . "<br/>";
echo "Not Connected";
die();
}
$query = $db->prepare("SELECT `nom`, `prenom`, `telephone`, `email`,`ncni`, `datenaissance`, `image` FROM `users` ORDER BY `id` DESC");
$query->execute();
if ($query->rowCount() > 0) {
$data = $query->fetchAll(PDO::FETCH_ASSOC);
echo json_encode($data);
}
else
{
echo 'No Result Found';
}
这是我的 php 文件,用于使用 android 中的 recycleview
检索用户信息。
但它不起作用我不知道。我需要协助
这里,image是TEXT和String
,其他都是varchar String
也在我的数据库里'xxxxx'table---users
<?php
include("connect.php");
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT nom, prenom, telephone, email,ncni, datenaissance, image FROM users ORDER BY id DESC ";
$result = $conn->query($sql);
if ($result->num_rows >0) {
while($row[] = $result->fetch_assoc()) {
$tem = $row;
$json = json_encode($tem);
}
} else {
echo "No Results Found.";
}
echo $json;
$conn->close();
?>
你的代码有很多错误。你跳过了 mysqli (query,fetch_assoc,num_rows should be mysqli_query,mysqli_fetch_assoc,mysqli_num_rows)无处不在。如果你不想多次写mysqli,你应该试试PDO。 这是一个例子:
try {
$db = new PDO('mysql:host=localhost;dbname=DBNAME;charset=utf8',DBUSER, DBPASS);
//echo "Connected";
}
catch (PDOException $e) {
//print "Error!: " . $e->getMessage() . "<br/>";
echo "Not Connected";
die();
}
$query = $db->prepare("SELECT `nom`, `prenom`, `telephone`, `email`,`ncni`, `datenaissance`, `image` FROM `users` ORDER BY `id` DESC");
$query->execute();
if ($query->rowCount() > 0) {
$data = $query->fetchAll(PDO::FETCH_ASSOC);
echo json_encode($data);
}
else
{
echo 'No Result Found';
}