Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    Regular Coder
    Join Date
    Feb 2012
    Posts
    123
    Thanks
    19
    Thanked 3 Times in 3 Posts

    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.

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,179
    Thanks
    75
    Thanked 4,339 Times in 4,305 Posts
    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.

  • Users who have thanked Old Pedant for this post:

    XCalibre (03-29-2012)

  • #3
    Regular Coder
    Join Date
    Feb 2012
    Posts
    123
    Thanks
    19
    Thanked 3 Times in 3 Posts
    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.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,179
    Thanks
    75
    Thanked 4,339 Times in 4,305 Posts
    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.

  • Users who have thanked Old Pedant for this post:

    XCalibre (03-29-2012)

  • #5
    Regular Coder
    Join Date
    Feb 2012
    Posts
    123
    Thanks
    19
    Thanked 3 Times in 3 Posts
    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.

  • #6
    Regular Coder
    Join Date
    Feb 2012
    Posts
    123
    Thanks
    19
    Thanked 3 Times in 3 Posts
    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>'; 
    			}

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,179
    Thanks
    75
    Thanked 4,339 Times in 4,305 Posts
    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.

  • Users who have thanked Old Pedant for this post:

    XCalibre (03-29-2012)

  • #8
    Regular Coder
    Join Date
    Feb 2012
    Posts
    123
    Thanks
    19
    Thanked 3 Times in 3 Posts
    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>';

  • #9
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,179
    Thanks
    75
    Thanked 4,339 Times in 4,305 Posts
    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.

  • Users who have thanked Old Pedant for this post:

    XCalibre (03-29-2012)

  • #10
    Regular Coder
    Join Date
    Feb 2012
    Posts
    123
    Thanks
    19
    Thanked 3 Times in 3 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •