Object of class mysqli could not be converted to string [closed]

-1

I'm having the following error:

  

Object of class mysqli could not be converted to string.

PHP code:

<?php

include("config.php");

 $query = $_GET['query']; 


    $min_length = 3;


    if(strlen($query) >= $min_length){ 

        $query = htmlspecialchars($query); 


        $query = mysqli_real_escape_string($conn,$query);

        $row_results = mysqli_query($conn,"SELECT * FROM books
            WHERE ('Title' LIKE '%".$query.$conn."%') OR ('text' LIKE '%".$query.$conn."%')") or die(mysqli_error());


        if(mysqli_num_rows($row_results) > 0){ 

            while($results = mysqli_fetch_array($row_results)){

                echo "<p><h3>".$results['title']."</h3>".$results['text']."</p>";

            }

        }
        else{ 
            echo "No results";
        }

    }
    else{ 
        echo "Minimum length is ".$min_length;
    }
?>

I do not understand what is missing

    
asked by anonymous 07.05.2017 / 00:34

1 answer

0

As extensively discussed in chat , the error is in using the object $conn within your query, as highlighted below:

$row_results = mysqli_query($conn, "SELECT * FROM books WHERE ('Title' LIKE '%".$query.$conn."%') OR ('text' LIKE '%".$query.$conn."%')") or die(mysqli_error());
                                                                                       ^^^^^                                 ^^^^^

It does not make sense for you to use the object to connect to the database within the query, so you must remove it. Another error that appeared was that you are trying to search the database in a text column that does not exist, ie you should remove this part of the code as well. Finally, another error in the code is that you forgot to pass the $conn object as a parameter of the mysqli_error function, at the end of this same line.

So, once corrected, the line should look like this:

$row_results = mysqli_query($conn, "SELECT * FROM books WHERE 'Title' LIKE '%".$query."%'") or die(mysqli_error($conn));
    
07.05.2017 / 16:10