Link to home
Start Free TrialLog in
Avatar of hnmcc
hnmcc

asked on

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

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?
ASKER CERTIFIED SOLUTION
Avatar of Beverley Portlock
Beverley Portlock
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of hnmcc
hnmcc

ASKER

Well it's obvious - once someone points it out...