kalster Posted May 25, 2013 Share Posted May 25, 2013 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 https://forums.phpfreaks.com/topic/278372-why-is-this-code-not-working-error-mysqli_fetch_assoc-expects-parameter-1-to-be-mysqli_result/ Share on other sites More sharing options...
xenLiam Posted May 25, 2013 Share Posted May 25, 2013 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 https://forums.phpfreaks.com/topic/278372-why-is-this-code-not-working-error-mysqli_fetch_assoc-expects-parameter-1-to-be-mysqli_result/#findComment-1432198 Share on other sites More sharing options...
kalster Posted May 25, 2013 Author Share Posted May 25, 2013 thanks for the help, but i still got the error. yes i verifyed the $link and it works. any other ideas Link to comment https://forums.phpfreaks.com/topic/278372-why-is-this-code-not-working-error-mysqli_fetch_assoc-expects-parameter-1-to-be-mysqli_result/#findComment-1432200 Share on other sites More sharing options...
kalster Posted May 25, 2013 Author Share Posted May 25, 2013 $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 https://forums.phpfreaks.com/topic/278372-why-is-this-code-not-working-error-mysqli_fetch_assoc-expects-parameter-1-to-be-mysqli_result/#findComment-1432201 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.