简体   繁体   中英

mysql_query function only works with mysql_num_rows

I have a very strange problem today. I have a section of code which queries a table based on a GET variable passed from a user input (pretty standard).
However for this to work I have to include a redundant variable.... 此工作有效,我必须包括一个冗余的变量。

This Works;

   <?php
   1. $cat = strval($_GET['c']);
   2. $query = "SELECT * FROM stock WHERE Category = '$cat'";
   3. $num_rows = mysql_num_rows(mysql_query($query)); 
   4. $values = mysql_query($query);
   ?>

For some reason without line 3 it doesn't work.
By the way this is ultimately used to create an array passed to a google chart which is all fine. What am I doing wrong?

All code for reference;

   <?php
   $cat = strval($_GET['c']);
   $query = "SELECT * FROM stock WHERE Category = '$cat'";
   LINE UNDER QUESTION --> $num_rows = mysql_num_rows(mysql_query($query)); 
   $values = mysql_query($query);
   $material = array();
   $quantity = array();
   $colour = array();
   while ($row = mysql_fetch_array($values)) {
       array_push($material, $row['Material']);
       array_push($quantity, $row['Quantity']);
       array_push($colour, $row['Colour']);
   }
   ...Then all the google chart stuff....
   ?>

Specify columns in the select and use an index. And make sure the $_GET value is validated. You probably already have done this, but try the query in eg phpMyAdmin. Maybe all this will help.

SELECT
    material,
    quantity,
    colour 
FROM
    stock
WHERE 
    Category = ...
ORDER BY ..
;

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM