Go Back   CodingForums.com > :: Server side development > MySQL

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-28-2012, 10:19 PM   PM User | #1
XCalibre
Regular Coder

 
Join Date: Feb 2012
Posts: 121
Thanks: 19
Thanked 3 Times in 3 Posts
XCalibre has a little shameless behaviour in the past
Warning: mysql_fetch_assoc() expects parameter 1

I've been at this for hours trying to figure this out. I get this error from my profile.php file.


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

This is the line number where the error says it is.


Code:
<a href="#" onclick="return false" onmousedown="javascript:toggleViewAllFriends('view_all_friends');">close </a>
I'm not to knowledgeable on mysql, so any further help would be greatly appreciated. Just let me know what else you need me to post. Thank you.

Last edited by XCalibre; 03-29-2012 at 01:19 AM..
XCalibre is offline   Reply With Quote
Old 03-28-2012, 10:33 PM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,210
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
No, that's the wrong line.

You need to look in the PHP file for the correct line, *NOT* in the HTML output.

What has happened is you have an error in your SQL code in a line that will look *SOMETHING* like this:
Code:
$result = mysql_query( $sql );
or
$result = mysql_query( "SELECT .... " );
and then the next bit of code calls mysql_fetch_assoc($result) but because of the error, $result (or whatever the variable name is) is a boolean false value.

In other words, because the query failed, you can't then try to get values returned by it.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Users who have thanked Old Pedant for this post:
XCalibre (03-29-2012)
Old 03-28-2012, 10:46 PM   PM User | #3
XCalibre
Regular Coder

 
Join Date: Feb 2012
Posts: 121
Thanks: 19
Thanked 3 Times in 3 Posts
XCalibre has a little shameless behaviour in the past
The line I have you came from the actual php file. That's why I don't understand the error. It doesn't take me to a mysql fetch anywhere. All I can do is guess here. Since it says in that php line where it's ALL FRIENDS, I must assume it's wanting the fetch from that table. Below is what code I have:

Code:
<?php 
    $sql = "SELECT * FROM friends_requests WHERE mem2='$id' ORDER BY id ASC LIMIT 50";
	$query = mysql_query($sql) or die ("Sorry we had a mysql error!");
	$num_rows = mysql_num_rows($query); 
	if ($num_rows < 1) {
		echo 'You have no Friend Requests at this time.';
	} else {
        while ($row = mysql_fetch_array($query)) { 
		    $requestID = $row["id"];
		    $mem1 = $row["mem1"];
	        $sqlName = mysql_query("SELECT username FROM myMembers WHERE id='$mem1' LIMIT 1") or die ("Sorry we had a mysql error!");
		    while ($row = mysql_fetch_array($sqlName)) { $requesterUserName = $row["username"]; }
As I stated, I"m very new to mysql scripting Thank you.
XCalibre is offline   Reply With Quote
Old 03-28-2012, 11:52 PM   PM User | #4
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,210
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Both of those queries *DO* have or die(...) so if the sql from them had an error, you would have gotten the "die" message.

Besides, read the error message again:
Code:
Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in
You aren't using mysql_fetch_assoc in any of that code you just showed.

The error *MUST* be where that function is called.

Are you using AJAX on your page? Possibly your JS code that does toggleViewAllFriends('view_all_friends') is invoking AJAX and that's getting the error?

But in any case, the error is clearly on a code line that uses mysql_fetch_assoc, none of which you have shown so far.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Users who have thanked Old Pedant for this post:
XCalibre (03-29-2012)
Old 03-29-2012, 12:15 AM   PM User | #5
XCalibre
Regular Coder

 
Join Date: Feb 2012
Posts: 121
Thanks: 19
Thanked 3 Times in 3 Posts
XCalibre has a little shameless behaviour in the past
That code did have a fetch on there:

while ($row = mysql_fetch_array($query)) {

That's why I thought it might be there. Here is the function I use:

Code:
function toggleViewAllFriends(x) {
		if ($('#'+x).is(":hidden")) {
			$('#'+x).fadeIn(200);
		} else {
			$('#'+x).fadeOut(200);
		}
Maybe an error in this code for links?

Code:
class autoActiveLink {

    function makeActiveLink($originalString){
        $newString = preg_replace('#(ht|f)tp(s)?://\w+/?#i', '<a href="\0">\0</a>', $originalString);
        return $newString;
    }
I"m at a loss....I'll look for more fetch arrays and post them.
XCalibre is offline   Reply With Quote
Old 03-29-2012, 12:18 AM   PM User | #6
XCalibre
Regular Coder

 
Join Date: Feb 2012
Posts: 121
Thanks: 19
Thanked 3 Times in 3 Posts
XCalibre has a little shameless behaviour in the past
Posted above too.... Thanks for looking...

Here's code for the friends:
Code:
$friendArray = explode(",", $friend_array);
	$friendCount = count($friendArray);
    $friendArray6 = array_slice($friendArray, 0, 6);
	
	$friendList .= '<div class="infoHeader">' . $username . '\'s Friends (<a href="#" onclick="return false" onmousedown="javascript:toggleViewAllFriends(\'view_all_friends\');">' . $friendCount . '</a>)</div>';
    $i = 0; // create a varible that will tell us how many items we looped over 
	 $friendList .= '<div class="infoBody" style="border-bottom:#666 1px solid;"><table id="friendTable" align="center" cellspacing="4"></tr>'; 
    foreach ($friendArray6 as $key => $value) { 
        $i++; // increment $i by one each loop pass 
		$check_pic = 'members/' . $value . '/image01.jpg';
		    if (file_exists($check_pic)) {
				$frnd_pic = '<a href="profile.php?id=' . $value . '"><img src="' . $check_pic . '" width="54px" border="1"/></a>';
		    } else {
				$frnd_pic = '<a href="profile.php?id=' . $value . '"><img src="members/0/image01.jpg" width="54px" border="1"/></a> &nbsp;';
		    }
			$sqlName = mysql_query("SELECT username, firstname FROM myMembers WHERE id='$value' LIMIT 1") or die ("Sorry we had a mysql error!");
		    while ($row = mysql_fetch_array($sqlName)) { $friendUserName = substr($row["username"],0,12); $friendFirstName = substr($row["firstname"],0,12);}
			if (!$friendUserName) {$friendUserName = $friendFirstName;} // If username is blank use the firstname... programming changes in v1.32 call for this
			if ($i % 6 == 4){
				$friendList .= '<tr><td><div style="width:56px; height:68px; overflow:hidden;" title="' . $friendUserName . '">
				<a href="profile.php?id=' . $value . '">' . $friendUserName . '</a><br />' . $frnd_pic . '
				</div></td>';  
			} else {
				$friendList .= '<td><div style="width:56px; height:68px; overflow:hidden;" title="' . $friendUserName . '">
				<a href="profile.php?id=' . $value . '">' . $friendUserName . '</a><br />' . $frnd_pic . '
				</div></td>'; 
			}
XCalibre is offline   Reply With Quote
Old 03-29-2012, 12:32 AM   PM User | #7
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,210
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Once again, look at the code:
Code:
while ($row = mysql_fetch_array($sqlName)
The error message was for mysql_fetch_assoc

Are you saying there is no instance of mysql_fetch_assoc anywhere in your code?

And you should also expect to find that function call after a mysql_query that is *NOT* followed by or die(...)
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Users who have thanked Old Pedant for this post:
XCalibre (03-29-2012)
Old 03-29-2012, 12:38 AM   PM User | #8
XCalibre
Regular Coder

 
Join Date: Feb 2012
Posts: 121
Thanks: 19
Thanked 3 Times in 3 Posts
XCalibre has a little shameless behaviour in the past
Sorry

Here's the code:

Code:
$usersql = mysql_query("SELECT firstname FROM mymembers WHERE id='$fromid'");
	while($row = mysql_fetch_assoc($usersql)){
	$fromuser = $row["firstname"];
	
				if (isset($_SESSION['idx'])){
				$blabberDisplayList .= '
			        <table style="background-color:#FFF; border:#999 1px solid; border-top:none;" cellpadding="5" width="100%">
					<tr>
					<td width="10%" valign="top">' . $newuserblabpic . '</td>
					<td width="90%" valign="top" style="line-height:1.5em;"><span class="greenColor textsize10">' . $whenBlab . ' <a href="profile.php?id=' . $fromid . '">' . $fromuser . '</a> said:</span><br />
            ' . $the_blab . '</td>
            </tr></table>';
XCalibre is offline   Reply With Quote
Old 03-29-2012, 12:48 AM   PM User | #9
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,210
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Okay!

So change that code to this:
Code:
$namesql = "SELECT firstname FROM mymembers WHERE id='$fromid'";

echo "<hr>DEBUG SQL: " . $namesql . "<hr/>";

$usersql = mysql_query( $namesql ) or die( mysql_error() );

while($row = mysql_fetch_assoc($usersql)){
That's really how you should always debug SQL in PHP. Echo out the query *before* executing it.

Then after the code starts working, just comment out the echo (so it's easy to put back in again if needed).

And *ALWAYS* have an or die after mysql_query or do a test for the result being false and then some other recovery code.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Users who have thanked Old Pedant for this post:
XCalibre (03-29-2012)
Old 03-29-2012, 01:09 AM   PM User | #10
XCalibre
Regular Coder

 
Join Date: Feb 2012
Posts: 121
Thanks: 19
Thanked 3 Times in 3 Posts
XCalibre has a little shameless behaviour in the past
Quote:
Originally Posted by Old Pedant View Post
Okay!

So change that code to this:
Code:
$namesql = "SELECT firstname FROM mymembers WHERE id='$fromid'";

echo "<hr>DEBUG SQL: " . $namesql . "<hr/>";

$usersql = mysql_query( $namesql ) or die( mysql_error() );

while($row = mysql_fetch_assoc($usersql)){
That's really how you should always debug SQL in PHP. Echo out the query *before* executing it.

Then after the code starts working, just comment out the echo (so it's easy to put back in again if needed).

And *ALWAYS* have an or die after mysql_query or do a test for the result being false and then some other recovery code.
Awesome. Thank you VERY much. And I appreciate the debugging tip. I've seen other say to debug it, but didn't see how. Thank you.

It ended up being that the mymebers table was not myMembers. LOL. I hate that. Hours of trying to figure it out and it's a stupid capital M LOL

Thanks again.
XCalibre is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:49 PM.


Advertisement
Log in to turn off these ads.