我会直截了当地说:
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|
我有2个班级:
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消息:
PDOStatement Object ( [queryString] => SELECT img_link, img_thumb FROM mycars_gallery WHERE car_id = :carid AND id IN(:imgids); )
我需要什么: 所有其他功能都正常工作,但对于这个特定的类似乎被打破了。绝对没有错误发生。我使用vardump时只显示查询,而不是获取图像链接和拇指。我觉得它与2个相互作用的类有关。
我已经完成了什么:
非常感谢先进。
答案 0 :(得分:2)
以下是您可能会发现有用的一些调查结果。首先来自PDO::prepare上的PHP手册。但是你使用implode()
传递一个字符串,但我认为这仍然是重要的事情。
您不能将多个值绑定到单个命名参数,例如,SQL语句的IN()子句。
第二和第三可能是由于拼写错误:
$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);";
并且,在您的表格结构中,您已将mycar_id
显示为列,但在where子句中有car_id
。
最后,我不想复制粘贴的可能解决方案,因此您可以实现以解决in
问题:
PHP PDO: Array in Update SQL WHERE IN () clause
Can I bind an array to an IN() condition?
<强>更新强>
这样的事情应该有效,但请记住$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
}