从MYSQL读取数据

时间:2012-12-08 09:48:27

标签: php mysql

这是我的php读取数据库文件:

 <?php
// read_My_Task.php


/*
 * Following code will get single task details
 * A task_detail is identified by task id (cid)
 */

// array for JSON response
$response = array();

// include db connect class
require_once __DIR__ . '/db_connect.php';

// connecting to db
$db = new DB_CONNECT();

// check for post data
if (isset($_GET["cid"])) {
    $cid = $_GET['cid'];

    // get a task from my-task table
    $result = mysql_query("SELECT *FROM my_task WHERE cid = $cid");

    if (!empty($result)) {
        // check for empty result
        if (mysql_num_rows($result) > 0) {

            $result = mysql_fetch_array($result);

            $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"];

            // user node
            $response["my_task"] = array();

            array_push($response["my_task"], $my_task);
            // 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);
        }
    } else {
        // no task found
        $response["success"] = 0;
        $response["message"] = "No task found";

        // echo no users JSON
        echo json_encode($response);
    }
} else {
    // required field is missing
    $response["success"] = 0;
    $response["message"] = "Required field(s) is missing";

    // echoing JSON response
    echo json_encode($response);
}

?>

数据库有很多数据但总是显示结果“{”成功“:0,”消息“:”必填字段缺失“}”

如何显示保存在MySql数据库中的数据

1 个答案:

答案 0 :(得分:0)

您的错误消息位于else脚本的末尾。如果您检查相应的if,则为:

if (isset($_GET["cid"])) {

这意味着你没有设置这个值。您只需要使用URL中的参数调用脚本,如下所示:

http://yourroot/read_My_Task.php?cid=XXX