Jump to content

PHP Warning: mysql_result() expects parameter 1 to be resource, boolean given in public_html/province.php on line 11


FUNKAM35
Go to solution Solved by Psycho,

Recommended Posts

<?php
$select="SELECT distinct province FROM property WHERE country = '$country' order by province asc "; 
$results = mysql_query("$select", $link_id);
if(mysql_num_rows($results)>0){
 while ($query_data = mysql_fetch_row($results))
 {
$theprovince=$query_data[0];
if(trim($theprovince==""))continue;
$theprovince=stripslashes($theprovince);
$total_results2 = mysql_result(mysql_query("SELECT COUNT(*) as Num  FROM property  WHERE province = '$theprovince' "),0) ;
echo "<li><a href=\"houses.php?province=$theprovince&country=$country\" title='$theprovince $country'>$theprovince ($total_results2)</a></li>\n";
}
}
?>
PHP Warning:  mysql_result() expects parameter 1 to be resource, boolean given in public_html/province.php on line 11

Hi, can anyone please advise as I keep getting this error message on this code and its driving me mad. Many thanks

Link to comment
Share on other sites

  • Solution

Your query is failing. And since you decided to group all the functions together you have no way to check for errors - not that you have added any error handling at all. But,the bigger issue is that you should NOT be running queries in loops - especially for what you have. You only need ONE query with a GROUP BY clause.

 

.josh is right about mysql_ functions being deprecated and you should change them, but here is a solution with what you currently have - which only uses a single query

 

 

<?php
$query = "SELECT province, COUNT(province) as total
          FROM property
          WHERE country = '{$country}'
          GROUP BY province
          ORDER BY province ASC";
$result = mysql_query($query, $link_id);
if(!$result)
{
    die("Query: {$query}<br>Error: " . mysql_error());
}

while ($row = mysql_fetch_assoc($result))
{
    //Assign variables
    $province = $row['province'];
    $total = $row['total'];
    $title = "{$province} {$country}";

    //Create output
    echo "<li><a href=\"houses.php?province={$province}&country={$country}\" title='{$title}'>{$province} ({$total})</a></li>\n";
}
?>
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.