Prepared语句与绑定变量不匹配,即使它确实存在?

时间:2017-06-11 04:29:50

标签: php mysql mysqli prepared-statement

据我所知,我从表中选择了6列,并且我绑定了6个变量,为什么它告诉我它不匹配?

这是一个人们可以投票的评级脚本。

这个脚本适用于插入/选择/联盟但是我注意到当人们从文件夹结构中投票选择图像时,一切都变得非常慢,所以我想将选定的图像移动到另一个文件夹,以便更快地访问它们,但是在尝试绑定结果时似乎遇到了问题。这对我来说通常不是问题。

有人可以向我解释为什么在这种情况下会发生这种情况吗?

更新

我忘了将类别变量SORRY包含在刚刚定义脚本的真实脚本中......那么为什么所有的downvotes?我告诉你SQL工作 - 我只是试图改进它。

$cat1 = 1;
$cat2 = 2:
$cat3 = 3;
$stmt = $dbCon->prepare(" INSERT INTO cars_daily ( cars_daily_identifier, cars_daily_source, cars_daily_views, cars_daily_votes, cars_daily_rating, cars_daily_category) "
        . " (SELECT cars_car_id, cars_car_source, cars_car_views, cars_car_votes, cars_car_rating, cars_car_category "
        . " FROM cars_bcar "
        . " WHERE cars_car_category IN (?) ORDER BY RAND() LIMIT 5) "
        . " UNION "
        . " (SELECT cars_car_id, cars_car_source, cars_car_views, cars_car_votes, cars_car_rating, cars_car_category "
        . " FROM cars_car "
        . " WHERE cars_car_category IN (?) ORDER BY RAND() LIMIT 5) "
        . " UNION "
        . " (SELECT cars_car_id, cars_car_source, cars_car_views, cars_car_votes, cars_car_rating, cars_car_category "
        . " FROM cars_car "
        . " WHERE cars_car_category IN (?) ORDER BY RAND() LIMIT 5) ");
$stmt->bind_param('iii', $cat1, $cat2, $cat3);
if ($stmt->execute()) {
    echo "<br><br>";
    echo "Success SELECT/INSERT";
    echo "<br><br>";
} else {
    echo "<br><br>";
    print_r($stmt->error);
    echo "<br><br>";
    echo "Fail SELECT/INSERT";
}

        $stmt->bind_result($cars_car_id, $cars_cars_source, $cars_car_views, $cars_car_votes, $cars_car_rating, $cars_car_category);
        while ($stmt->fetch()) {

            copy("models/$cars_cars_source", "daily/$cars_cars_source");
        }

1 个答案:

答案 0 :(得分:1)

原因

即使要插入的值来自 SELECT 语句,INSERT查询也不会产生记录(如SELECT)。

解决方案

一个选项是运行 INSERT 查询,然后运行单独的 SELECT 来获取插入的值。

$selectSQL = " (SELECT cars_car_id, cars_car_source, cars_car_views, cars_car_votes, cars_car_rating, cars_car_category "
    . " FROM cars_bcar "
    . " WHERE cars_car_category IN (?) ORDER BY RAND() LIMIT 5) "
    . " UNION "
    . " (SELECT cars_car_id, cars_car_source, cars_car_views, cars_car_votes, cars_car_rating, cars_car_category "
    . " FROM cars_car "
    . " WHERE cars_car_category IN (?) ORDER BY RAND() LIMIT 5) "
    . " UNION "
    . " (SELECT cars_car_id, cars_car_source, cars_car_views, cars_car_votes, cars_car_rating, cars_car_category "
    . " FROM cars_car "
    . " WHERE cars_car_category IN (?) ORDER BY RAND() LIMIT 5) ";
//run the INSERT statement
$stmt = $dbCon->prepare(" INSERT INTO cars_daily ( cars_daily_identifier, cars_daily_source, cars_daily_views, cars_daily_votes, cars_daily_rating, cars_daily_category) " . $selectSQL);
$stmt->bind_param('iii', $cat1, $cat2, $cat3);
if ($stmt->execute()) {
    echo "<br><br>";
    echo "Success SELECT/INSERT";
    echo "<br><br>";
} else {
    echo "<br><br>";
    print_r($stmt->error);
    echo "<br><br>";
    echo "Fail SELECT/INSERT";
}
$stmt->close(); //close the prepared statement
//run the SELECT statement
$stmt = $dbCon->prepare($selectSQL);
$stmt->bind_param('iii', $cat1, $cat2, $cat3);
$stmt->execute()
$stmt->bind_result($cars_car_id, $cars_cars_source, $cars_car_views, $cars_car_votes, $cars_car_rating, $cars_car_category);

    .