Solved

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

Posted on 2011-03-12
2
1,304 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Are You Using the Best Web Development Editor?

The worlds of web hosting and web development are constantly evolving. Every year we see design trends change, coding standards adapt and new frameworks/CMS created. With such a quick pace of change it’s easy to get lost trying to keep up.

See if your editor made the list.

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

631 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