Printing result of mysql query from variable

后端 未结 4 1975
日久生厌
日久生厌 2021-02-07 15:58

So I wrote this earlier (in php), but everytime I try echo $test\", I just get back resource id 5. Does anyone know how to actually print out the mysql query from the variable?<

相关标签:
4条回答
  • 2021-02-07 16:35

    This will print out the 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());
    print $query;
    

    This will print out the results:

    $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";
    }
    
    0 讨论(0)
  • 2021-02-07 16:40

    well you are returning an array of items from the database. so you need something like this.

       $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 '];
    }
    
    0 讨论(0)
  • 2021-02-07 16:43
    $sql = "SELECT * FROM table_name ORDER BY ID DESC LIMIT 1";
    $records = mysql_query($sql);
    

    you can change LIMIT 1 to LIMIT any number you want

    This will show you the last INSERTED row first.

    0 讨论(0)
  • 2021-02-07 16:50

    From php docs:

    For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error.

    For other type of SQL statements, INSERT, UPDATE, DELETE, DROP, etc, mysql_query() returns TRUE on success or FALSE on error.

    The returned result resource should be passed to mysql_fetch_array(), and other functions for dealing with result tables, to access the returned data.

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

    0 讨论(0)
提交回复
热议问题