Jump to content

why is this code not working... error, mysqli_fetch_assoc() expects parameter 1 to be mysqli_result


kalster

Recommended Posts

i know that there is data in the table but i am getting this error when there is no data. i verifyed that $username is not null. the error is at line 2

	$sql = "SELECT * FROM users WHERE username = " . $username;
	$result = mysqli_query($link, $sql);
	$row = mysqli_fetch_assoc($result);
	while(mysqli_num_rows($result) >0) {
		echo $row['id'];
	}

I am guessing that $username is a string, and therefore it needs to be enclosed in quotation marks. Although I may be wrong. Try:

$sql = "SELECT * FROM users WHERE username='{$username}';";

and see if that will work for you. Also, are you sure that $link is a valid resource and does not return a FALSE flag?

$sql= "SELECT * FROM users WHERE username='11'";
$result=mysqli_query($link, $sql) or die ("couldn't execute query.");
while($row = mysqli_fetch_array($result)){
	$username = $row['id'];
echo $username;
}

the above code works. i just forget to assign the variable :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.