Object of class mysqli could not be converted to string

Asked

Viewed 916 times

-1

I’m having the following mistake:

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 don’t understand what’s missing

  • What would those be $query.$conn within the query?

  • mysqli requires two parameters, in this case the first one is $Conn the connection to the database and the second one is SELECT statement

  • I edited the answer, now you have the whole code

  • Yes, but it makes no sense to have the object of connection to the database in the middle of the select. Try to remove $conn where is $query.$conn.

  • "SELECT * FROM books WHERE Title LIKE '%".$query."%' OR text LIKE '%".$query."%'"

  • if I remove $Conn, error, mysqli says it needs 2 parameters

  • Which $conn you took?

  • what’s behind the SELECT

  • See my comment above. I passed exactly what is the $conn that you should withdraw. It should not be WITHIN the instruction SELECT.

Show 5 more comments

1 answer

0


As widely 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 makes no sense for you to use the database connection object within the query, so you should remove it. Another error that appeared was that you are trying to perform a database search in a column text which does not exist, ie should remove this part of the code as well. Finally, another error in the code is that you forgot to pass the object $conn as a function parameter mysqli_error, 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));

Browser other questions tagged

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