0

I'm running into a situation where a mysql_query() result being fed into a mysql_fetch_array() function is being interpreted as a boolean instead of the result.

The below code uses Using an SQL result in a foreach loop as a coding example for doing a foreach loop. There may be multiple problems with the code still as my current problem occurs before the foreach loop.

$results=mysql_query("SELECT * FROM order_details WHERE orderid = $orderid");

    print "SELECT * FROM order_details WHERE orderid = $orderid";

    $productid;
    $quantity;
    $price;

    $resultset = array();
while ($row = mysql_fetch_arraY($results)) {
$resultset[] = $row;
}

    foreach ($resultset as $result)
    {

    $productid = $result['productid'];
    $quantity = $result['quantity'];
    $price = $result['price'];

    print "<br />$productid, $quantity, $price";
    };
3
  • It is not “interpreted as boolean” – it simply is boolean false, because your query has an error. Use mysql_error() to find out what it is. Commented Dec 4, 2013 at 23:22
  • By building SQL statements with outside variables, you are leaving yourself wide open to SQL injection attacks. Also, any input data with single quotes in it, like a name of "O'Malley", will blow up your SQL query. Please learn about using parametrized queries, preferably with the PDO module, to protect your web app. bobby-tables.com/php has examples to get you started, and this question has many examples in detail. Commented Dec 5, 2013 at 17:24
  • Thank you, I realize that this is not perfect. But is just some quick code for a PHP class. I would most certainly sanitize my strings in a real world environment. :) Commented Dec 8, 2013 at 6:00

2 Answers 2

0

Change $orderid to '$orderid' provided that everything is fine. One big note, try going over mysqli and PDO instead of mysql.

Sign up to request clarification or add additional context in comments.

Comments

0
for($i=0;$i<$max;$i++) {
        $pid=$_SESSION['cart'][$i]['productid'];
        $q=$_SESSION['cart'][$i]['qty'];
        $price=get_price($pid);

        $pname;

        $row = mysql_fetch_assoc(mysql_query("SELECT name\n"
        . "FROM `products` \n"
        . "WHERE SERIAL =$pid\n"
        . "LIMIT 1"));
        $pname = $row['name'];

        print "<br  />Product Name: $pname, Quantity: $q, Price: $price";
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.