简体   繁体   English

PHP Mysqli准备不创建语句对象

[英]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. 我正在使用带有准备好的语句的php mysqli扩展名,但是在一个查询中它并未创建查询对象。

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)) 该行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. if()返回false,因此不会创建$query对象,也不会执行if内部的任何代码。

The echo $sqlq; 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=?" “选择id,从productcategories中命名,在productcategories.productid = products.id上加入产品,在那里categoryid ==或categoryid =?或categoryid =?或categoryid =?或categoryid =?或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=? SELECT id,name from productcategories在productcategories.productid = products.id上加入产品WHERE categoryid =? OR categoryid=? 或categoryid =? OR categoryid=? 或categoryid =? OR categoryid=? 或categoryid =? OR categoryid=? 或categoryid =? OR categoryid=? 或categoryid =?

Should have been 本来应该

SELECT productcategories.id,products.name,products.id FROM productcategories JOIN products ON productcategories.productid = products.id WHERE categoryid=? 从productcategories中选择productcategories.id,products.name,products.id在productcategories.productid = products.id上将产品加入JOIN WHERE categoryid =? OR categoryid=? 或categoryid =? OR categoryid=? 或categoryid =? OR categoryid=? 或categoryid =? OR categoryid=? 或categoryid =? OR categoryid=? 或categoryid =?

Hey Nico. 嗨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. 我不确定该查询将多久运行一次,或者可以追加多少个类别,但是您可能需要考虑使用WHERE IN语法。

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) (向MySQL发送较少的数据,在PHP中发送较小的字符串)

I don't think you actually call the prepare on the connection object rather just the mysqli object itself. 我认为您实际上没有在连接对象上调用prepare,而只是在mysqli对象本身上调用了prepare。

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 http://php.net/manual/zh/mysqli.prepare.php

Edit2: 编辑2:

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=?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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