Solved

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

Posted on 2011-03-12
2
1,178 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
object oriented programming comparison 5 54
Sql query on a varchar that is numeric. 8 26
PHP - AJAX and MySQL it works only if the value is a number 12 34
datetime in sql 6 24
Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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.

911 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now