简体   繁体   English

PDO数据库不返回行,只返回查询字符串(pdo对象)

[英]PDO Database doesn't return rows, only returns query string (pdo object)

I'm gonna go straight to the point: 我要直截了当地说:

Mysql DB tbl = mycars_gallery Mysql DB tbl = mycars_gallery

|id|  car_id|img_thumb |img_link  |
-----------------------------------
|5 |    3   |thumb1.jpg|image1.jpg| 
|6 |    3   |thumb2.jpg|image2.jpg| 
|7 |    3   |thumb3.jpg|image3.jpg| 

I have 2 classes: 我有2个班:

Upload.class.php Upload.class.php

class Upload
{
function __construct() 
{
    // nothing
}

function deleteCarImgs($car_id,$arr_imgids)
{   
    //weld all img ids and separate by commas
    $img_ids = implode(",",$arr_imgids);

    $sql = "SELECT img_link, img_thumb ";
        .= "FROM mycars_gallery WHERE car_id = :carid AND id IN(:imgids);"; 

    $params = array(
        ":carid" => $car_id,
        ":imgids" => $img_ids
    );

    //DB processing happens in Database class(100% working)
    $dbconn = new Database;     
    $rows = $dbconn->dbProcess($sql, $params); //doesnt run
}
}

Cars.class.php Cars.class.php

class Cars
{
function __construct() 
{
    // nothing
}

function deleteCompleteCar(3)//car_id is 3
{
    $upload = new Upload();

    $arr_imgids = array(5,6,7);//these are img_id = 5,6,7 the ones I want to select
    $res = $upload->deleteCarImgs($id,$arr_imgids);

    var_dump($res);
}
}

var_dump message: var_dump消息:

PDOStatement Object (     [queryString] => SELECT img_link, img_thumb FROM mycars_gallery WHERE car_id = :carid AND id IN(:imgids); )   

What I need: All other functions are working correctly, but for this particular classes it seems to be broken. 我需要的是:所有其他功能都正常工作,但对于这个特定的类,它似乎被打破了。 There are absolutely no errors that happen. 绝对没有错误发生。 Instead of getting the image links and thumbs, only query shows up when I use vardump. 我使用vardump时只显示查询,而不是获取图像链接和拇指。 I have a feeling it has to do with 2 classes interacting with each other. 我觉得它与2个相互交互的类有关。

What I've already done: 我已经做了什么:

  1. I've ran sql query directly many times, it works 我已经多次直接运行sql查询,它的工作原理
  2. I ran Database PDO functions through other classes, it works 我通过其他类运行数据库PDO函数,它的工作原理
  3. I also tried to get rid of implode() and passing a single digit string '7' to :imgsid token, still no luck, it actually returned NULL, instead of the PDO obj 我也试图摆脱implode()并将单个数字字符串'7'传递给:imgsid令牌,仍然没有运气,它实际上返回NULL,而不是PDO obj

Thanks a lot in advanced. 非常感谢先进。

Here are a few findings that you might find useful. 以下是您可能会发现有用的一些调查结果。 First is from the PHP manual on PDO::prepare . 首先是来自PDO :: prepare的PHP手册。 But you are passing a single string using implode() , but I think this is still something important to keep in mind. 但是你使用implode()传递一个字符串,但我认为这仍然是重要的事情要记住。

You cannot bind multiple values to a single named parameter in, for example, the IN() clause of an SQL statement. 您不能将多个值绑定到单个命名参数,例如,SQL语句的IN()子句。

Second and third are probably due to typo: 第二和第三可能是由于拼写错误:

$sql = "SELECT img_link, img_thumb "; // this semi colon will cause syntax error on the next line.
     .= "FROM mycars_gallery WHERE car_id = :carid AND id IN(:imgids);"; 

and, in your table structure you've shown mycar_id as a column but you have car_id in your where clause. 并且,在您的表结构中,您已将mycar_id显示为列,但在where子句中有car_id

Finally, possible solution which I didn't want to copy paste, so here is something you could implement to solve the in issue: 最后,可能的解决办法,我不想复制粘贴,所以这里是你可以实现解决in问题:

PHP PDO: Array in Update SQL WHERE IN () clause PHP PDO:更新SQL WHERE IN()子句中的数组

Can I bind an array to an IN() condition? 我可以将数组绑定到IN()条件吗?

Update: 更新:

Something like this should work, but please keep in mind that $arr_imgids needs to be an array for this to work: 这样的东西应该可以工作,但请记住$arr_imgids需要是一个数组才能工作:

function deleteCarImgs($car_id, $arr_imgids)
{
    $in_sql = ':imgid' . implode(',:imgid', $arr_imgids);

    $sql = "SELECT img_link, img_thumb 
            FROM mycars_gallery WHERE car_id = :carid AND id IN({$in_sql});"; 

    $params = array_merge(
        array(":carid" => $car_id), 
        array_combine(explode(',', $in_sql), $arr_imgids)
    );

    //DB processing happens in Database class(100% working)
    $dbconn = new Database;     
    $rows = $dbconn->dbProcess($sql, $params); //doesnt run
}

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

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