My problem is that in the first code below it doesn't display the data on browser, just shows a black page. There are no errors in the logs.
Another thing this displays the data using var_dump or print_r but then it doesn't encode in the right json format.
Is there a way of echoing the response? On the second code I follow exactly the same procedure as the first and it shows me the data I want without var_dum or print_r.
<?php
/*
* Following code will list all the products
*/
// array for JSON response
$response = array();
// include db connect class
require_once __DIR__ . '/conectaDB.php';
// connecting to db
$db = new DB_CONNECT();
// check for post data
// get all products from products table
$result = mysql_query("select cd.id, cd.dt_ini_camp, cd.dt_fim_camp, cd.descricao, (select sum(quantidade) from lin_doc where id_cab_doc = cd.id) as qtd from cab_doc cd where cd.id_tipo_doc = 1") or die(mysql_error());
// check for empty result
if (mysql_num_rows($result) > 0) {
// looping through all results
// products node
$response["cab_doc"] = array();
while ($row = mysql_fetch_array($result)) {
var_dump($row);
// temp user array
$linha = array();
$linha["id"] = $row["id"];
$linha["dt_ini_camp"] = $row["dt_ini_camp"];
$linha["dt_fim_camp"] = $row["dt_fim_camp"];
$linha["descricao"] = $row["descricao"];
$linha["qtd"] = $row["qtd"];
// push single product into final response array
array_push($response["cab_doc"], $linha);
}
// success
$response["success"] = 1;
// echoing JSON response
echo json_encode($response);
} else {
// no products found
$response["success"] = 0;
$response["message"] = "No products found";
// echo no users JSON
echo json_encode($response);
}
?>
The output of this code is a blank page.
The below works fine and displays the data:
<?php
/*
* Following code will list all the products
*/
// array for JSON response
$response = array();
// include db connect class
require_once __DIR__ . '/conectaDB.php';
// connecting to db
$db = new DB_CONNECT();
// check for post data
if (isset($_POST["id"])) {
$id = $_POST['id'];
// get all products from products table
$result = mysql_query("SELECT id, id_estab, dt, hr, quantidade FROM lin_doc WHERE id_cab_doc = $id") or die(mysql_error());
// check for empty result
if (mysql_num_rows($result) > 0) {
// looping through all results
// products node
$response["lin_doc"] = array();
while ($row = mysql_fetch_array($result)) {
//var_dump($row);
// temp user array
$linha = array();
$linha["id"] = $row["id"];
$linha["id_estab"] = $row["id_estab"];
$linha["dt"] = $row["dt"];
$linha["hr"] = $row["hr"];
$linha["quantidade"] = $row["quantidade"];
// push single product into final response array
array_push($response["lin_doc"], $linha);
}
// success
$response["success"] = 1;
// echoing JSON response
echo json_encode($response);
} else {
// no products found
$response["success"] = 0;
$response["message"] = "No products 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);
}
?>
Output of second code:
{"lin_doc":[{"id":"66","id_estab":"12","dt":"2013-03-26","hr":"16:30:00","quantidade":"373.000"},{"id":"148","id_estab":"16","dt":"2013-07-08","hr":"20:45:00","quantidade":"119.000"},{"id":"260","id_estab":"14","dt":"2012-09-20","hr":"20:00:00","quantidade":"324.000"},{"id":"275","id_estab":"33","dt":"2012-07-23","hr":"22:00:00","quantidade":"489.000"},{"id":"306","id_estab":"25","dt":"2012-07-27","hr":"20:45:00","quantidade":"338.000"},{"id":"468","id_estab":"15","dt":"2013-04-23","hr":"20:45:00","quantidade":"263.000"},{"id":"546","id_estab":"18","dt":"2013-05-22","hr":"11:15:00","quantidade":"87.000"},{"id":"578","id_estab":"11","dt":"2013-03-06","hr":"13:30:00","quantidade":"30.000"},{"id":"601","id_estab":"07","dt":"2013-02-23","hr":"20:00:00","quantidade":"87.000"},{"id":"617","id_estab":"14","dt":"2012-11-23","hr":"20:45:00","quantidade":"234.000"},{"id":"651","id_estab":"03","dt":"2013-05-12","hr":"20:45:00","quantidade":"311.000"},{"id":"752","id_estab":"06","dt":"2013-01-13", "hr":"13:30:00","quantidade":"219.000"},{"id":"803","id_estab":"22","dt":"2013-02-23","hr":"16:30:00","quantidade":"396.000"},{"id":"819","id_estab":"06","dt":"2012-10-21","hr":"18:15:00","quantidade":"333.000"},{"id":"850","id_estab":"03","dt":"2013-01-19","hr":"14:30:00","quantidade":"265.000"},{"id":"899","id_estab":"22","dt":"2013-06-15","hr":"13:00:00","quantidade":"453.000"},{"id":"993","id_estab":"10","dt":"2013-02-03","hr":"19:30:00","quantidade":"441.000"}], "success":1}
In your connect: mysql_set_charset('utf8', $link);
try:
<?php
$link = mysql_connect('db_host', 'db_user', 'db_password');
mysql_set_charset('utf8', $link);
$db_selected = mysql_select_db('your_db', $link);
or encode your array:
foreach( $row as $value ) {
$value = utf8_encode( $value );
}
I'm not sure if your query is ok in this point:
"as qtd from cab_doc cd where cd.id_tipo_doc = 1"
^^^^
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.