-1

I have the following old PHP code that worked fine:

<?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>"
?>

I want to convert it to mysqli. I did the following to convert it but it is not working:

<?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>"
?>

What am I doing wrong?

user1822824
  • 2,478
  • 6
  • 41
  • 65

1 Answers1

3

If you have the mysqlnd driver installed or you are using PHP 8.1, you can do this:

$all_rows = mysqli_fetch_all($result, MYSQLI_ASSOC);
$note1 = $all_rows[0]["note_content"];
$note2 = $all_rows[1]["note_content"];

If not, you have to fetch each row separately:

$row = mysqli_fetch_assoc($result);
$note1 = $row["note_content"];
$row = mysqli_fetch_assoc($result);
$note2 = $row["note_content"];
Dharman
  • 30,962
  • 25
  • 85
  • 135
Barmar
  • 741,623
  • 53
  • 500
  • 612