0

Here is my code

$itemSelect="select * from items where gender='men' and type='suite'";
$itemQuery=mysql_query($itemSelect) or die(mysql_error());
$num=count($itemQuery);
echo $num;
if($itemQuery)
{
    echo "I am here";
    echo "<table>";
    while($row = mysql_fetch_array($itemQuery))
    {
            $photo=$row['photo'];
            $name=$row['name'];
            $price=$row['price'];
            echo "<tr><td>". $photo ."</td><td>" .$name ."</td><td>" .$price ."</td>     </tr>";
    }
    echo "</table>";
}
else
{
    echo "There is some mistacke";
    die();
}

So here $num=count($itemQuery); shows me that there is 1 item that satisfies the search but the loop is never executed what could be the problem? Thanks in advance.

5
  • 1
    WHy do you all people dont like foreach loop? isnt it easier to under stand? foreach ($itemQuery as $value) { } Commented Mar 17, 2014 at 11:02
  • @Michael Because you can't iterate over a mysql result resource...?! Commented Mar 17, 2014 at 11:06
  • @Michael If you see clearly $itemQuery is a resultset and he is fetching the records, foreach will not make a difference. @OP can you do a echo mysql_error(); after executing the query? Commented Mar 17, 2014 at 11:07
  • What happens if you replace count($itemQuery) with mysql_num_rows($itemQuery). Also, are you sure the loop doesn't execute? Maybe photo, name and price are NULL? Commented Mar 17, 2014 at 11:11
  • Hm, that is weird it returns me 0 :( Commented Mar 17, 2014 at 11:21

2 Answers 2

2

The reason you are getting 1 is because of this code:

$num=count($itemQuery);
echo $num;

count is a function to get the number of items in an array. If you pass anything that isn't an array (or doesn't implement the ICountable interface), then count returns always 1.

So this piece of code is incorrect in that it shows 1, while that number isn't at all related to the number of rows returned.

The query itself looks fine to me, although I cannot validate it against your actual database. If you see the message "I am here", then your query executed fine, but didn't return any rows. If you don't see that message, then something else went wrong. You can use mysql_error to try to find any errors in that case.

3
  • mysql_num_rows would be the right one :) --> $num=mysql_num_rows($itemQuery); Commented Mar 17, 2014 at 11:12
  • OK that makes sense thanks, but to be honest there is an item in my database that satisfies the search so why the loop still doesn't run? Commented Mar 17, 2014 at 11:13
  • 1
    When there is no mysql_error, check your SQL string and your WHERE. Is the gender really "men"? Commented Mar 17, 2014 at 11:16
0
$count= mysql_num_rows($itemQuery);
echo $count;
if($count > 0)){
    echo "<table>";
    while($row = mysql_fetch_array($itemQuery)){
        $photo=$row['photo'];
        $name=$row['name'];
        $price=$row['price'];
        echo "<tr><td>". $photo ."</td><td>" .$name ."</td><td>" .$price ."</td>     </tr>";
    }
    echo "</table>";
}else{
    echo "there are no results";
    die();
}

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.