php - fetch_assoc() errors

Asked

Viewed 78 times

0

Good,

I created this code to fetch all data through a SEARCH.

If I write for example "Olaaaaa", it does not error and shows the fishing, but if I give space and excrever for example "Olaaa Souuuuu", it shows this error.

Fatal error: Call to a member function fetch_assoc() on a non-object in D:\xampp\htdocs\Superfacil_v1.2.8.9\inc\class_search.php on line 32

Line 32 is basically this

    $smtp_category_cc = "SELECT count(*) as total FROM public_ads WHERE $construct";
    $smtp_category_qry_cc = $con->query($smtp_category_cc);
    $total_request = $smtp_category_qry_cc->fetch_assoc(); 

total code is

    $name = mysqli_real_escape_string($con, sanitize($_GET['search']));
$category = mysqli_real_escape_string($con, sanitize($_GET['category']));
$x = 0; 
$q = str_replace(array("\\",";"), "", $name);  // remove ALL backslashes & remove ALL ";" -> for sql security: no (simple) injection of commands
$q = trim($q);
$search_exploded = explode(" ", $q);

foreach($search_exploded as $search_each ) { 

    $x++; 
    $construct = " "; 

    if($x == 1) { 
        $construct .= "ads_title LIKE '%$search_each%' AND category_id = '$category' AND ads_active = 1 AND ads_end = 0"; 
    } else {     
        $construct .= "AND ads_brand LIKE '%$search_each%' AND category_id = '$category' AND ads_active = 1 AND ads_end = 0"; 
    }
} 

/**************************/
$smtp_category_cc = "SELECT count(*) as total FROM public_ads WHERE $construct";
$smtp_category_qry_cc = $con->query($smtp_category_cc);
$total_request = $smtp_category_qry_cc->fetch_assoc();     

echo '<p class="shop-results">Encontrados <span class="badge">'. $total_request['total'] .'</span> <strong>An&uacute;ncios</strong>. </p>';      
/**************************/    


$get_search = "SELECT ads_id, client_id, category_id, ads_title, ads_brand, ads_content, ads_price, ads_views, ads_image_1, ads_image_2, ads_image_3, ads_date FROM public_ads WHERE $construct";
$get_search_qry = $con->query($get_search);


if($get_search_qry->num_rows > 0) {

Could you tell me what this mistake is?

========================== ERROR 2:

erro:You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'AND ads_brand LIKE '%dsadsa%' AND category_id = '31' AND ads_active = 1 AND ads_' at line 1
  • do this in your code $smtp_category_qry_cc = $con->query($smtp_category_cc) or die('erro:' . $con->error);

  • It is probably due to lack of whitespace when you concatenate the strings to mount SQL. Just check the value of $smtp_category_cc.

  • missed was this , I go above

  • And the value of the variable I mentioned?

  • Yes is the error value of the variable that said

1 answer

0


Spaces are missing at the beginning and end of the string, otherwise they will join/concatenate and it will do something that was meant to be like:

SELECT ... WHERE foo='bar' AND baz='bar' AND bar='bar'

turn:

SELECT ... WHERE foo='bar'ANDbaz='bar'ANDbar='bar'

and the $construct = " "; also has to stay out of the foreach

$construct = " "; 

foreach($search_exploded as $search_each ) { 

    $x++; 

    if($x == 1) { 
        $construct .= " ads_title LIKE '%$search_each%' AND category_id = '$category' AND ads_active = 1 AND ads_end = 0 "; 
    } else {     
        $construct .= " AND ads_brand LIKE '%$search_each%' AND category_id = '$category' AND ads_active = 1 AND ads_end = 0 "; 
    }
}

Off that can add the or die to catch the error (you can use if also):

$smtp_category_qry_cc = $con->query($smtp_category_cc) or die('erro:' . $con->error);

Another way to solve the loop of foreach to add items to Where would be an array, for example:

$wherearr = array();

foreach($search_exploded as $search_each ) { 

    $x++; 

    if($x == 1) { 
        $wherearr[]= "ads_title LIKE '%$search_each%' AND category_id = '$category' AND ads_active = 1 AND ads_end = 0"; 
    } else {     
        $wherearr[]= "ads_brand LIKE '%$search_each%' AND category_id = '$category' AND ads_active = 1 AND ads_end = 0"; 
    }
}


$construct = implode(' AND ', $wherearr);//Junta a array separada por espaços em uma string
  • No more mistakes, but there is a record with "Levada House", and I put Levada House he does not find, just finds as House or Levada... this is normal?

  • Is there any way to improve the code ?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.