0

I am trying to retrieve the number of rows based on this query in wordpress:

protected function wp_has_facility($fid)
{
    //global $wpdb;

    $fid = intval($fid);

    $sql = "SELECT post_id FROM wprf_postmeta WHERE meta_value = '".$fid."' AND meta_key = 'facility_id'";
    $result = mysql_query($sql) or die(mysql_error());

    $num_rows = mysql_num_rows($result);

    echo $num_rows;

    return $num_rows;
}

$num_rows returns 0 when this function is ran.

If I echo the query string and run it in phpmyadmin, it selects successfully giving me a number of rows.

After further investigation it looks like when I hardcode the meta_value it will give me a row count. But if I'm populating the string with variables it doesn't work.

Any ideas?

12
  • Have you got any entries with meta_key "thing_id" actually, or was it just a placeholder for a var, i.e. $thing_id? Commented Sep 4, 2012 at 17:41
  • Are you sure that there are datasets which met both conditions? Beside that, try out the query in PHPMyAdmin. Commented Sep 4, 2012 at 17:41
  • A query can be successful and return 0 rows. Obviously, your AND condition limits the query to 0 rows...i.e, nothing matches the meta_key you are passing it. Try running your queries directly w/ phpMyAdmin or MySQL Workbench to verify. Commented Sep 4, 2012 at 17:41
  • What is thing_id? Did you mean this: $result = mysql_query( "SELECT * FROM wprf_postmeta WHERE meta_value = 123 AND meta_key = '$thing_id'") or die(mysql_error()); $num_things = mysql_num_rows($result); Commented Sep 4, 2012 at 17:45
  • I'm sorry I should have mentioned - I did try it in phpmyadmin with the AND statement and it does return rows, along with a row count. However in PHP it does not Commented Sep 4, 2012 at 17:50

1 Answer 1

8

This only means your table currently contains no record that satisfies both criteria.

Remarks about your code:

Concatenation is not necessary, you may replace the query with:

$sql = "SELECT post_id FROM wprf_postmeta WHERE meta_value = '$fid' AND meta_key = 'facility_id'";

mysql_* functions are deprecated, you should avoid using them as they will be removed in a future version of PHP. Here are the alternatives.

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

7 Comments

The same query in phpmyadmin gave me matching records
I copied and pasted the full query string from my PHP script into phpmyadmin. PHP giving me num_rows of 0, phpmyadmin giving me 1.
@PhillTidd Please copy and paste it here as well. Try to echo the SQL string, not rownum.
@dezso SELECT post_id FROM wprf_postmeta WHERE meta_value = '21' AND meta_key = 'facility_id'
@PhillTidd are you sure that $fid does not contain a space or something? And a sidenote: does meta_value contain numeric values? What is its type? Is it different from wp_postmeta?
|

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.