views:

51

answers:

1

I'm using prepared statements and MySQLi, and am currently getting this error.

PHP Catchable fatal error:  Object of class mysqli_stmt could not be converted to string in /Users/me/path/to/project/ModelBase/City.php on line 31

The code it's coming from (full function):

    function select($query, $limit)
{
    $query = "%".$query."%";
    $con = ModelBase::getConnection();
    $sql = "SELECT name FROM cities WHERE name LIKE ? LIMIT ?";
    $query = $con->prepare($sql) or die("Error preparing sql in City ".parent::$db_conn->error);
    $query->bind_param("si", $query, $limit) or die("Error binding params in City ".parent::$db_conn->error);
    $query->execute() or die("Error executing query in City");

    $tmp = "";
    $res = $query->bind_result($tmp);
    while($query->fetch());
    {
        $citylist[] = $tmp;
    }
    $query->close();
}

Like 31 is the $query->execute(). I can't find any information about this, and this is almost identical syntax to other systems I've built and never had this issue.

+1  A: 

I think the problem is that you are using $query in two contexts. Once as a SQL string, and once to prepare your statement:

$query = "%".$query."%";
...
$query = $con->prepare($sql) 

so when you run this line:

 $query->bind_param("si", $query <--- bind to itself?

execution of the actual query will crash.

Pekka
Of all the stupid simple things, thanks.
Josh K