简体   繁体   中英

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

|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:

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

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:

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. I have a feeling it has to do with 2 classes interacting with each other.

What I've already done:

  1. I've ran sql query directly many times, it works
  2. I ran Database PDO functions through other classes, it works
  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

Thanks a lot in advanced.

Here are a few findings that you might find useful. First is from the PHP manual on PDO::prepare . But you are passing a single string using implode() , but I think this is still something important to keep in mind.

You cannot bind multiple values to a single named parameter in, for example, the IN() clause of an SQL statement.

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.

Finally, possible solution which I didn't want to copy paste, so here is something you could implement to solve the in issue:

PHP PDO: Array in Update SQL WHERE IN () clause

Can I bind an array to an IN() condition?

Update:

Something like this should work, but please keep in mind that $arr_imgids needs to be an array for this to work:

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
}

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