Warning: mysql_fetch_assoc(): supplied argument is not...

Hi All,

I’ve been having issues with my PHP, so I switched everything to the mysql_* format, instead of using mysqli (that wasn’t working). Anyway, I finally get everything to work and I get this error:

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/mycontra/public_html/member.php on line 54
Warning: mysql_free_result(): supplied argument is not a valid MySQL result resource in /home/mycontra/public_html/member.php on line 62

I’ve tried messing with the query syntax, and even pasted it from phpMyAdmin, and I’m still getting the error. Can anyone help me figure out why? The format all looks correct to me, and I was able to log in to this site, so i know the DB and tables are working.

[code]$user_email=$_SESSION[‘valid_user’];

					$conn = mysql_connect('localhost','username','password'); 
						if (!$conn)
							throw new Exception('Could not connect to database server');
						else
							return $conn;


					$db_selected = mysql_select_db('mycontra_main', $conn);
					if (!$db_selected)
							die ('Can't use mycontra_main : ' . mysql_error());
					
					echo '</p><strong>Your Recent Contractors</strong><p>';
					
					echo '<table cellpadding="3" cellspacing="1" border="0" bgcolor="#666666">
				<tr>
					<td bgcolor="#D2DBF6">Contractor Name</td><td bgcolor="#D2DBF6">License Number</td><td bgcolor="#D2DBF6">Location</td><td bgcolor="#D2DBF6">Your Rating</td>
				</tr>';
					
					$query = mysql_query("SELECT * FROM contractors WHERE email = '".$user_email."' GROUP BY license", $conn);
					
					while ($line = mysql_fetch_assoc($query)) //This is line 54
				
						{
								echo'<tr><td bgcolor="#FFFFFF"><a href="contractor.php?id='.$line['id'].'&name='.$line['lastname'].'&license='.$line['license'].'" target="_parent">'.$line['firstname'].'&nbsp;'.$line['lastname'].'</a></td>';
								echo'<td bgcolor="#FFFFFF">'.$line['license'].'</td>';
								echo'<td bgcolor="#FFFFFF">'.$line['city'].',&nbsp;'.$line['state'].'&nbsp;'.$line['zipcode'].'</td>';
								echo'<td bgcolor="#FFFFFF">'.$line['rating'].'</td></tr>';
							}
					echo '</table><p style="margin-bottom: 15px;">';
					mysql_free_result($query); //This is line 62[/code]

Thanks for the help,
Jen

try echoing a mysql_error() message JUST AFTER the mysql_query line. This might give some reasons as to WHY the query is not returning a valid argument.

What I usually do is the following:

$sql = "SELECT * FROM table WHERE foo = ".$bar;
$rslt = mysql_query($sql);

This gives me to option to echo the $sql variable (the actual SQL query) if things don’t go as expected. It makes your code longer, but it makes it just a tad easier to debug ;)

Sponsor our Newsletter | Privacy Policy | Terms of Service