Reputation: 3
I create php file to get all task from database but when I enter the url "localhost/evisiting/get_all_task.php" in browser it get all task details but with null data in each row. However I populate my database table. Kindly guide how can i get these values from database not the null values..
Its my php file:
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);
}
?>
Upvotes: 0
Views: 113
Reputation: 360562
You're using the wrong variable inside your while() loop. $result
is your query result handle, NOT the row you fetched:
$my_task["cid"] = $result["cid"];
^^^^^^^--- should be $row
As well, you're spitting out a lot of code to fetch individual fields when you could simply have this:
$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;
}
exact same end-result, but far less repetition of all those field names - if you want to add a new field into this result, you simply at it to the SELECT statement, and the rest of the code handles it automatically. If you need to change the field name in the $response array, simply alias it in the query.
Upvotes: 1