Solved

PHP Warning:  mysql_fetch_assoc() expects parameter 1 to be resource, boolean given...

Posted on 2011-03-12
2
1,242 Views
Last Modified: 2012-05-11
I am working on a recently-inherited legacy database.  One of the tables is structured in a way that is unsuitable for its purpose, so I need to take the data and place it in another table that is suitable.

I have created the new table: four TINYINT columns that will be set either as 0 (default) or 1.  A sufficient number of rows exists showing default values.  I intend to update each row by scrolling through the relevant data from the old table.

Unfortunately, I can't loop through the data because I keep getting the warning above (it's the same with mysql_fetch_array).

The query is good: if I cut and paste it into phpMyAdmin I get c.39,000 rows returned, as expected.

The connection is good: it's all handled by the same includes used in the working system.  To check, I have added error-handling from the working pages, and that reports no errors.

The PHP version of the query seems to be good - if I comment out the while() loop and substitute print_r, it tells me that the mysql_query is returning Resource id #5.

But the loop doesn't loop.  It puts values from the first row retrieved into $ue and $hpc; and it does update the correct row in the new table as it should.  But that's it (apart from throwing the Warning above).

	//Retrieve relevant contents of original table
	$sql = mysql_query("SELECT `user_entry_FK`, `hpc_standard_FK` FROM member_standards ORDER BY `user_entry_FK`");
//	print_r ($sql);

	//Amend fields in target table
 	while ( $array  = mysql_fetch_assoc($sql) ) {
 		$ue = $array['user_entry_FK'];
 		$hpc = $array['hpc_standard_FK'];
 		switch ($hpc) {
 			case 1:
 			$sql = mysql_query ("UPDATE user_HPC SET `hpc1` = 1 WHERE `enid_FK` =$ue");
 			break;
 
 			case 2:
 			$sql = mysql_query ("UPDATE user_HPC SET `hpc2` = 1 WHERE `enid_FK` =$ue");
 			break;
 
 			case 3:
 			$sql = mysql_query ("UPDATE user_HPC SET `hpc3` = 1 WHERE `enid_FK` =$ue");
 			break;
 
 			case 4:
 			$sql = mysql_query ("UPDATE user_HPC SET `hpc4` = 1 WHERE `enid_FK` =$ue");
 			break;
 		}
 	}

Open in new window


Where am I going wrong?
0
Comment
Question by:hnmcc
2 Comments
 
LVL 34

Accepted Solution

by:
Beverley Portlock earned 500 total points
ID: 35116196
Your are storing the resource ID in $sql on line 2 of the fragment above. Inside the loop you then assign $sql a new value based on the UPDATE query thus wiping out your resource ID.

Assign the queries in the loop to some other variable. For UPDATE queries only TRUE or FALSE is returned, not a resource id so on the next loop iteration $sql contain a boolean which throws the error message.
0
 

Author Closing Comment

by:hnmcc
ID: 35116233
Well it's obvious - once someone points it out...
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question