Jump to content


Photo

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


Best Answer kalster, 25 May 2013 - 12:42 AM

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

Go to the full post


  • Please log in to reply
3 replies to this topic

#1 kalster

kalster

    Advanced Member

  • Members
  • PipPipPip
  • 74 posts

Posted 24 May 2013 - 09:57 PM

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'];
	}


#2 xenLiam

xenLiam

    Advanced Member

  • Members
  • PipPipPip
  • 43 posts
  • LocationManila
  • Age:16

Posted 25 May 2013 - 12:00 AM

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?


Lives by faith, thinks by logic.


#3 kalster

kalster

    Advanced Member

  • Members
  • PipPipPip
  • 74 posts

Posted 25 May 2013 - 12:18 AM

thanks for the help, but i still got the error. yes i verifyed the $link and it works. any other ideas


Edited by kalster, 25 May 2013 - 12:26 AM.


#4 kalster

kalster

    Advanced Member

  • Members
  • PipPipPip
  • 74 posts

Posted 25 May 2013 - 12:42 AM   Best Answer

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






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com