3

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.

Tatarin
  • 1,238
  • 11
  • 28
  • If you're deleting rows, you won't get a result of some number of rows; instead the db will just report what it did. Only on select will you get rows back. – Nathaniel Ford Aug 22 '13 at 01:33
  • @Nathaniel Ford I know, that's why i'm using select only to check if query will run – Tatarin Aug 22 '13 at 02:52

1 Answers1

2

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
}
Community
  • 1
  • 1
vee
  • 38,255
  • 7
  • 74
  • 78
  • mycar_id was a typo, already fixed it. Also, I tried overwriting implode with regular single string - didn't work as well. I will look into those articles. – Tatarin Aug 22 '13 at 03:05
  • Okay, after playing around and moving code. I think I found the original issue. I needed to fetch object to see content. Which is all good and well. You were right about IN() not liking tokens with multi values, even if it's the same string. I did get values, but IN() part is still not working, what can I do? or is there a replacement for that func? – Tatarin Aug 22 '13 at 03:26
  • @Timur, please see the update. Something like that should work. – vee Aug 22 '13 at 04:32
  • Bingo! I tried your solution and it worked. Later on today, i will pick apart your code to fully understand how you did it. Thanks! – Tatarin Aug 22 '13 at 13:08