Solved

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

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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.

747 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

12 Experts available now in Live!

Get 1:1 Help Now