在php数据库中查找特定值

在php数据库中查找特定值,php,mysqli,Php,Mysqli,我尝试过不同的方法,但似乎都不管用。当我在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) $

我尝试过不同的方法,但似乎都不管用。当我在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++;
}
我得到了预期的项目路径,但由于某种原因,它返回了一个空字符串

$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++;
}
提前感谢您的帮助(如果您愿意的话)

编辑:


我试图将绑定更改为另一个变量,但仍然得到一个空字符串。我不确定这是因为我包装数据不正确还是因为其他原因。我一直在寻找其他类似的事情发生,但我什么也没想到。

您将语句绑定到子句之外,并且从未使用您更改的
$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++;
}
$search=$mysqli->prepare(“从item\u title=?”所在的items中选择item\u路径”);
while(数组\键\存在($i,$parts)){
//包含(word1、word2、word3)的数组
$data=$parts[$i];
//将mysqli查询的数据包装到“”
$data=“\”.$data。“\”;
//把它绑在这里!
$search->bind_参数(“s”,$data);
//这应该执行上面的准备并给我数据
$search->execute();
$search->bind\u result($img\u path);
$search->fetch();
//这将返回一个空字符串
echo$img_路径;
回声“;
$i++;
}

我尝试将其绑定到While循环中,但仍然无法工作。我还使用$search->execute();这应该使用更改后的$data。接受您和octern的建议,它成功了,所以谢谢。您可以在执行查询之前尝试将$img_path设置为默认值。这会告诉你绑定是否失败,或者它是否成功并写入了一个空字符串。啊,一个好主意,我刚刚尝试了,是的,它返回了一个空字符串。这让我觉得我的质疑是错误的,但我不确定如何正确引用一个项目。我很确定我做得不对。试着运行它,但不要将数据用引号括起来。准备好的查询不需要引用。事实上,我认为在字符串中添加引号会导致SQL搜索带有引号的字符串,这可能是您的查询失败的原因。哦,我非常确定这是我尝试的第一件事,但弄乱了它,去掉了“”部分,我让它工作了,非常感谢您!
$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++;
}