我有以下旧版PHP代码可以正常工作:
<?php
$query = "SELECT * FROM notes WHERE note_name='Test Note'";
$result = mysql_query($query);
$note1 = mysql_result($result, 0, "note_content");
$note2 = mysql_result($result, 1, "note_content");
echo "<p>$note1</p>
<p>$note2</p>"
?>
我想将其转换为mysqli。我做了以下转换,但它无法正常工作:
<?php
$query = "SELECT * FROM notes WHERE note_name='Test Note'";
$result = mysqli_query($connect, $query);
// THIS IS WHERE I AM UNSURE WHAT TO DO??? HERE'S WHAT I TRIED
$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
$note1 = $row["note_content"][0];
$note2 = $row["note_content"][1];
echo "<p>$note1</p>
<p>$note2</p>"
?>
我做错了什么?
答案 0 :(得分:2)
如果您安装了mysqlnd
驱动程序,则可以执行以下操作:
$all_rows = mysqli_fetch_all($result, MYSQLI_ASSOC);
$note1 = $all_rows[0]["note_content"];
$note2 = $all_rows[1]["note_content"];
如果没有,则必须分别获取每一行:
$row = mysqli_fetch_assoc($result);
$note1 = $row["note_content"];
$row = mysqli_fetch_assoc($result);
$note2 = $row["note_content"];