3 Replies - 16919 Views - Last Post: 12 March 2011 - 12:06 PM Rate Topic: -----

#1 betsy221  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 09-July 08

php warning mysql_fetch_assoc expects parameter 1 to be resource boole

Posted 12 March 2011 - 11:03 AM

the code gives the warning given in the topic title

but the database is updated and the logic works perfectly every time i refresh.. and everytime it gives the same warning

<?php

$conn = mysql_connect('localhost' , 'root' , 'admin')
	or die('Error connecting to MySql server.');
	
	mysql_select_db("timepass",$conn);
	
	
	$sql1 = "SELECT * from student WHERE allocated=0";
	$result1 = mysql_query($sql3,$conn);
	
	
	$sql2 = "SELECT * from seats";
	$result2 = mysql_query($sql2,$conn);
	
	if($result1)
	{
		$number=mysql_num_rows($result3);
	}
	
	for($counter=0; $counter<($number+1); $counter++)
	{
		$sql3 = "SELECT DISTINCT * from student WHERE allocated=0 LIMIT 1";
		$result3 = mysql_query($sql3,$conn);
	
		if(mysql_num_rows($result3)!=0)
		{
		
		while($row1 = mysql_fetch_assoc($result3))
		{	
			if(mysql_num_rows($result2)!=0)
			{
			while($row2 = mysql_fetch_assoc($result2))
			{
				if($row1['pref1']==$row2['prefCode'])
				{
					if($row2['availableSeats'] > 0)
					{	
						echo $i;
						$fno = $row1['formno'];
						$pref = $row1['pref1'];
						$sql3 = "UPDATE student SET allocated = '1',alloc_code = '$pref'  WHERE formno = '$fno'";
						
						$result3 = mysql_query($sql3,$conn);
						$sql4 ="UPDATE seats SET availableSeats = availableSeats-1,allocatedSeats=allocatedSeats+1 WHERE prefCode='$pref'";
						$result4 = mysql_query($sql4,$conn);
					}
					else
					{
						continue;
					}
					break;
				}
				elseif($row1['pref2']==$row2['prefCode'])
				{
					if($row2['availableSeats'] > 0)
					{	
						$fno = $row1['formno'];
						$pref = $row1['pref2'];
						$sql3 = "UPDATE student SET allocated = '1',alloc_code = '$pref'  WHERE formno = '$fno'";
						
						$result3 = mysql_query($sql3,$conn);
						$sql4 ="UPDATE seats SET availableSeats = availableSeats-1,allocatedSeats=allocatedSeats+1 WHERE prefCode='$pref'";
						$result4 = mysql_query($sql4,$conn);
					}	
					else
					{
						continue;
					}	
					break;
				}
			}
			}
		}
		}
	}
?>


MOD EDIT: When posting code...USE CODE TAGS!!!

:code:

This post has been edited by JackOfAllTrades: 12 March 2011 - 12:01 PM


Is This A Good Question/Topic? 0
  • +

Replies To: php warning mysql_fetch_assoc expects parameter 1 to be resource boole

#2 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3719
  • View blog
  • Posts: 5,991
  • Joined: 08-June 10

Re: php warning mysql_fetch_assoc expects parameter 1 to be resource boole

Posted 12 March 2011 - 11:22 AM

This warning is telling you that at the specified line, you are trying to read a row from a MySQL resource that did not return a result set.

A MySQL query call can return either a result set, or a boolean indicating the success of the query. If a query that you expect to return a result set fails, it will return FALSE.

So before you ever use a result set from a MySQL query, you need to make sure it is not FALSE:
$result = mysql_query($sql, $connection);
if ($result)
{
    # And do the mysql_fetch_assoc here.
}
else
{
    # If the code depends on this returning a result set
    # use an else clause to show an error. If the code can
    # go on without this data, you can just let it go.
}


Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6075
  • View blog
  • Posts: 23,540
  • Joined: 23-August 08

Re: php warning mysql_fetch_assoc expects parameter 1 to be resource boole

Posted 12 March 2011 - 12:02 PM

Question #3 in the PHP FAQs topic.
Was This Post Helpful? 0
  • +
  • -

#4 betsy221  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 09-July 08

Re: php warning mysql_fetch_assoc expects parameter 1 to be resource boole

Posted 12 March 2011 - 12:06 PM

View PostAtli, on 12 March 2011 - 11:22 AM, said:

This warning is telling you that at the specified line, you are trying to read a row from a MySQL resource that did not return a result set.

A MySQL query call can return either a result set, or a boolean indicating the success of the query. If a query that you expect to return a result set fails, it will return FALSE.

So before you ever use a result set from a MySQL query, you need to make sure it is not FALSE:
$result = mysql_query($sql, $connection);
if ($result)
{
    # And do the mysql_fetch_assoc here.
}
else
{
    # If the code depends on this returning a result set
    # use an else clause to show an error. If the code can
    # go on without this data, you can just let it go.
}



thank you so much
the warning is gone
:)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1