简体   繁体   中英

mysql_num_rows() return 0 when using AND

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?

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 .

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