如何回应这种查询

时间:2014-12-08 09:03:16

标签: php mysql

我搜索一些查询来计算我的数据库中同一行的值我在product_descriptiontable delevery,其中您可以看到所有产品的列表和quantity在哪里可以看到列出产品的数量。

我找到了这段代码,但我不知道如何回应这个我的网页?

SELECT quantity, count(*) as 'count quantity' 
FROM delivery 
GROUP BY product_description

2 个答案:

答案 0 :(得分:0)

试试这个。它会将JSON返回给您的应用程序

<?php

$DB_NAME = 'DATABASE_NAME';
$DB_HOST = 'DATABASE_HOST';
$DB_USER = 'DATABASE_USER';
$DB_PASS = 'DATABASE_PASSWORD';

$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
$yourValue = $request->yourValue;

$mysqli = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);

$sql="SELECT quantity, count(*) as 'quantity' FROM delivery GROUP BY product_description";

$result = $mysqli->query($sql) or die($mysqli->error.__LINE__);

if($result === FALSE) {
    die(mysql_error()); // TODO: better error handling
}

$result1 = array();
    while($row = mysql_fetch_array($result)){
    array_push($result1, $row);
}

echo json_encode($result1);

&GT;

答案 1 :(得分:0)

try to show your result with function var_dump();
$query="SELECT quantity, count(*) as 'quantity' FROM delivery GROUP BY product_description";


$result = mysql_query($query);



var_dump($result);