php mysqli没有返回值

时间:2014-09-14 23:56:11

标签: php mysql mysqli

以下是给我一个问题的代码:

$letter = $_POST['artistButton'];
$result = $mysqli->query("SELECT * FROM `Generic` WHERE `songName` LIKE '$letter%'");
$row    = $result->fetch_array();

while ($row = $result->fetch_array()) {
    $Artist = $row['Artist'];
    $songName = $row['songName'];
    $Duration = $row['Duration'];
    $URL = $row['URL'];
    $Genre = $row['Genre'];
    echo "<input type='submit' value='$songName' name='artistButton'>";
    echo "<br />";
}

声明是正确的我将它保存到一个变量并将其打印到屏幕上,然后从数据库中运行它并打印出我想要的结果,所以它绝对不是那样的。出于某种原因虽然没有显示。我猜这与我搞砸了从mysql转换为mysqli的东西有关。谢谢 - 山姆

1 个答案:

答案 0 :(得分:1)

显然有些代码会被删除,但请尝试删除额外代码:

$row = $result->fetch_array();

并使用:

while($row = $result->fetch_assoc()) {

因为你对关联指数感兴趣。

重要说明:由于您使用的是mysqli_*,请使用其参数化查询。请勿在查询中直接使用$_POST值。

$letter = "{$_POST[artistButton]}%";
$stmt = $mysqli->prepare('SELECT * FROM `Generic` WHERE `songName` LIKE ?');
$stmt->bind_param('s', $letter);
$stmt->execute();

$result = $stmt->get_result();

while($row = $result->fetch_assoc()) {
    $Artist = $row['Artist'];
    $songName = $row['songName'];
    $Duration = $row['Duration'];
    $URL = $row['URL'];
    $Genre = $row['Genre'];
    echo "<input type='submit' value='$songName' name='artistButton'>";
    echo "<br />";
}