从变量打印mysql查询的结果

时间:2011-09-22 18:20:58

标签: php mysql

所以我之前写过(在php中),但每次我尝试echo $ test“,我只是回到资源id 5.有谁知道如何从变量中实际打印出mysql查询?

$dave= mysql_query("SELECT order_date, no_of_items, shipping_charge, SUM(total_order_amount) as test FROM `orders` WHERE DATE(`order_date`) = DATE(NOW()) GROUP BY DATE(`order_date`)") or die(mysql_error());
print $dave;

4 个答案:

答案 0 :(得分:18)

这将打印出查询:

$query = "SELECT order_date, no_of_items, shipping_charge, SUM(total_order_amount) as test FROM `orders` WHERE DATE(`order_date`) = DATE(NOW()) GROUP BY DATE(`order_date`)";

$dave= mysql_query($query) or die(mysql_error());
print $query;

这将打印出结果:

$query = "SELECT order_date, no_of_items, shipping_charge, SUM(total_order_amount) as test FROM `orders` WHERE DATE(`order_date`) = DATE(NOW()) GROUP BY DATE(`order_date`)";

$dave= mysql_query($query) or die(mysql_error());

while($row = mysql_fetch_assoc($dave)){
    foreach($row as $cname => $cvalue){
        print "$cname: $cvalue\t";
    }
    print "\r\n";
}

答案 1 :(得分:5)

你正在从数据库中返回一系列项目。所以你需要这样的东西。

   $dave= mysql_query("SELECT order_date, no_of_items, shipping_charge, 
    SUM(total_order_amount) as test FROM `orders` 
    WHERE DATE(`order_date`) = DATE(NOW()) GROUP BY DATE(`order_date`)") 
    or  die(mysql_error());


while ($row = mysql_fetch_assoc($dave)) {
echo $row['order_date'];
echo $row['no_of_items'];
echo $row['shipping_charge'];
echo $row['test '];
}

答案 2 :(得分:1)

来自php docs:

  

对于SELECT,SHOW,DESCRIBE,EXPLAIN和其他语句返回   resultset,mysql_query()在成功时返回资源,或者返回FALSE   错误。

     

对于其他类型的SQL语句,INSERT,UPDATE,DELETE,DROP等,   mysql_query()成功时返回TRUE,错误时返回FALSE。

     

返回的结果资源应该传递给mysql_fetch_array(),   以及用于处理结果表的其他函数,以访问   返回数据。

http://php.net/manual/en/function.mysql-query.php

答案 3 :(得分:0)

$sql = "SELECT * FROM table_name ORDER BY ID DESC LIMIT 1";
$records = mysql_query($sql);
你可以改变     限制1 至    限制你想要的任何数字

这将首先显示最后一个INSERTED行。