Jump to content

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


kalster
Go to solution Solved by 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'];
	}
Link to comment
Share on other sites

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?

Link to comment
Share on other sites

  • Solution
$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 :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.