[英]database: get null result
我创建了php文件来从数据库中获取所有任务,但是当我在浏览器中输入url“ localhost / evisiting / get_all_task.php”时,它将获取所有任务详细信息,但每一行中都包含空数据。 但是我填充我的数据库表。 请指导如何从数据库而不是空值中获取这些值。
它是我的php文件:
get_all_task.php
<?php
/*
* Following code will list all the tasks
*/
// array for JSON response
$response = array();
// include db connect class
require_once __DIR__ . '/db_connect.php';
// connecting to db
$db = new DB_CONNECT();
// get all task from my_task table
$result = mysql_query("SELECT *FROM my_task") or die(mysql_error());
// check for empty result
if (mysql_num_rows($result) > 0) {
// looping through all results
// task node
$response["my_task"] = array();
while ($row = mysql_fetch_array($result)) {
// temp user array
$my_task = array();
$my_task["cid"] = $result["cid"];
$my_task["cus_name"] = $result["cus_name"];
$my_task["contact_number"] = $result["contact_number"];
$my_task["ticket_no"] = $result["ticket_no"];
$my_task["task_detail"] = $result["task_detail"];
// push single task into final response array
array_push($response["my_task"], $my_task);
}
// success
$response["success"] = 1;
// echoing JSON response
echo json_encode($response);
} else {
// no task found
$response["success"] = 0;
$response["message"] = "No task found";
// echo no users JSON
echo json_encode($response);
}
?>
您在while()循环中使用了错误的变量。 $result
是您的查询结果句柄,而不是您获取的行:
$my_task["cid"] = $result["cid"];
^^^^^^^--- should be $row
同样,当您只需拥有以下内容时,您就会吐出很多代码来获取各个字段:
$result = mysql_query("SELECT cid, cus_name, contact_number, .... FROM my_task") or die(mysql_error());
$response['my_task'] = array();
while($row = mysql_fetch_assoc($result)) {
$response['my_task'][] = $row;
}
最终结果完全相同,但所有这些字段名称的重复次数却很少-如果要将新字段添加到此结果中,只需将其添加到SELECT语句中,其余代码将自动对其进行处理。 如果您需要更改$ response数组中的字段名称,只需在查询中为其添加别名即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.