1

I've been working on my chat app some more, and I got it inserting into MySQL fine. I apparently had the wrong engine. Anyways, it's wanting "mysqli_result" instead of "mysqli_fetch_assoc". I know this code is fine considering it works fine on another page.

I've tried retyping, copying the working code and putting my values, anything I could really think of... Here's my code

$query='mysqli_query'($con, "SELECT * FROM chat ORDER by id ASC");
if ($query = true) {
while($row = mysqli_fetch_assoc($query)) {
    $username=$row["username"];
    $text=$row["text"];
    $time=date('G:i', strtotime($row["time"])); 

echo "<p>$time | $username: $text</p>";
}

I expected the code to work since I copied it from a working bit of code, but instead I got this, " Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given in /storage/ssd2/266/7895266/public_html/read.php on line 15 "

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Grizzly
  • 66
  • 8

1 Answers1

0

As pointed out, remove the quotes around the function call.

// Incorrect syntax
$query='mysqli_query'($con, "SELECT * FROM chat ORDER by id ASC");

// Correct
$query = mysqli_query($con, "SELECT * FROM chat ORDER by id ASC");

But you also have another issue:

if ($query = true) {

Should be:

if ($query) {

or even better:

if ($query instanceof mysqli_result) {

Use comparison instead of assignment. By using an equals sign, you are making $query always evaluate to true and the if condition passes. The comparison should be checking for a non-falsy value -- specifically a mysqli_result object.

Jeremy Harris
  • 24,318
  • 13
  • 79
  • 133