简体   繁体   中英

PHP Mysqli prepared not creating statement object

I am using the php mysqli extension with prepared statements, but on one query it is not creating the query object.

Here is the code:

$sqlq = "SELECT id,name FROM productcategories JOIN products ON productcategories.productid = products.id WHERE categoryid=?";
if($allsubcats)
{
    foreach($allsubcats as $key => $data)
    {
        $sqlq .= " OR categoryid=?";
    }
}
echo $sqlq;
if($query = $this->mysqli->connection->prepare($sqlq))
{
    $query->bind_param("i", $cat);
    if($allsubcats)
    {
        foreach($allsubcats as $key => $data)
        {
            $query->bind_param("i", $data[0]);
        }
    }
    $query->execute();
    $query->bind_result($id,$name);
    $query->store_result();
    if($query->num_rows > 0)
    {
        while($row = $query->fetch())
        {
            $allprods["id"] = $id;
            $allprods["name"] = $name;
        }
    }
    $query->close();
}

The problem:

The line if($query = $this->mysqli->connection->prepare($sqlq))
The if() is returning false, and therefore not creating the $query object, and not executing any of the code inside the if.

The echo $sqlq; returns:

"SELECT id,name FROM productcategories JOIN products ON productcategories.productid = products.id WHERE categoryid=? OR categoryid=? OR categoryid=? OR categoryid=? OR categoryid=? OR categoryid=?"

Which I don't see anything wrong with.

Any help would be greatly appreciated,

Thanks, Nico

Typical, I worked it out myself as soon as I posted this, does anyone else see things better as soon as they ask for help??

Anyway,

SELECT id,name FROM productcategories JOIN products ON productcategories.productid = products.id WHERE categoryid=? OR categoryid=? OR categoryid=? OR categoryid=? OR categoryid=? OR categoryid=?

Should have been

SELECT productcategories.id,products.name,products.id FROM productcategories JOIN products ON productcategories.productid = products.id WHERE categoryid=? OR categoryid=? OR categoryid=? OR categoryid=? OR categoryid=? OR categoryid=?

Hey Nico. This isn't an answer to your question, as you already answered it. Just an unsolicited suggestion. I'm not sure how often that query will be run, or how many categories could be appended to it, but you may want to consider using the WHERE IN syntax.

Instead of:

WHERE foo = ? OR foo = ? OR foo = ? OR foo = ?

Use:

WHERE foo IN (?,?,?,?)

You'll make the queries more readable, and save a miniscule amount of time in your application. (sending less data to MySQL, smaller strings in PHP)

I don't think you actually call the prepare on the connection object rather just the mysqli object itself.

ie


if($query = $this->mysqli->connection->prepare($sqlq))

should just be


if($query = $this->mysqli->prepare($sqlq))

Edit
Checkout the first example:
http://php.net/manual/en/mysqli.prepare.php

Edit2:

Ahh yes i see.

As a side note you should consider aliasing your tables:


SELECT pc.id,p.name,p.id
FROM productcategories pc
JOIN products p ON pc.productid = p.id
WHERE categoryid=? OR categoryid=? OR categoryid=? OR categoryid=? OR categoryid=? OR categoryid=?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM