0

我尝试了不同的方法,但似乎都没有奏效。当我在 MySQL 控制台中尝试该命令时,它可以正常工作,但是当我在这个 PHP 脚本中尝试它时,它会向我发送一个空字符串。

$search = $mysqli->prepare("SELECT item_path FROM items WHERE item_title = ?");

while (array_key_exists($i, $parts)) {
    // An array that contains (word1, word2, word3)
    $data = $parts[$i];

    // Wraps data in "" for mysqli query
    $data = "\"" . $data . "\"";

    // Binding here still doesn't work
    $search->bind_param("s", $data);

    // This should execute the above prepare and give me the data
    // from the way I think it work's this execute() should use the updated $data
    $search->execute();
    $search->bind_result($img_path);
    $search->fetch();

    // This returns an empty string
    echo $img_path;
    echo "<img src= \"", $img_path, "\" >";

    $i++;
}

如果我进入 MySQL 控制台并运行:

SELECT item_path FROM items WHERE item_title = "word1"

我得到了我期待的 item_path,但由于某种原因,我的脚本返回了一个空字符串。

提前感谢您的帮助(如果您愿意的话)!

编辑:

    // Wraps data in "" for mysqli query
    $data2 = "\"" . $data . "\"";

    // Binding here still doesn't work even after changing the var
    $search->bind_param("s", $data2);

我厌倦了将绑定更改为另一个变量,但我仍然得到一个空字符串。我不确定这是否是因为我错误地包装了数据,或者是否还有其他原因。我一直在寻找其他类似的事情发生,但我一无所获。

4

1 回答 1

1

您将语句绑定在子句之外,并且永远不会使用$data您更改的语句。

$search = $mysqli->prepare("SELECT item_path FROM items WHERE item_title = ?");

while (array_key_exists($i, $parts)) {
    // An array that contains (word1, word2, word3)
    $data = $parts[$i];

    // Wraps data in "" for mysqli query
    $data = "\"" . $data . "\"";

    // Bind it here instead!
    $search->bind_param("s", $data);

    // This should execute the above prepare and give me the data
    $search->execute();
    $search->bind_result($img_path);
    $search->fetch();

    // This returns an empty string
    echo $img_path;
    echo "<img src= \"", $img_path, "\" >";

    $i++;
}
于 2012-06-22T11:06:13.460 回答