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 6 of 6
  1. #1
    New Coder
    Join Date
    Aug 2010
    Posts
    35
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Please please help me

    here is the announcement file for my website...its working when i myself enters particular number(s) .but when I select send to all it gives error :


    Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/username/public_html/testing/include/class_database.php on line 140

    Warning: Invalid argument supplied for foreach() in /home/username/public_html/testing/admin/admin_announcements.php on line 22


    here is the Php code for the file please help me I tried a lot to debug it..
    PHP Code:
    {include file='admin_header.tpl'}

    <
    h2>SMS Announcements</h2>
    Use 
    this form to compose an sms to be sent to every registered user on the sms website
    <
    br /><br />
    {if 
    $result == 1}
    <
    div class='success'><img src='../images/success.gif' border='0' class='icon'SMS has been sent to all users.</div>
    {/if}
    <
    form action="admin_announcements.php" method="post" name="sms_form" id="sms_form">
    Enter Mobile Numbers below (commaseperated):<br />
    <
    textarea name="sms_mobiles" id="sms_mobiles" cols="50" rows="4"></textarea><br />
    <
    input type="checkbox" name="sendtoall" id="sendtoall" value="1" /> <label for="sendtoall">Send to all users</label><br /><br />
    Enter SMS Message Below:<br />
    <
    textarea name="sms_message" id="sms_message" cols="50" rows="4" onkeyup="check_sms();"></textarea><br /><br />
    <
    input type="text" name="chars_left" id="chars_left" readonly="1" disabled="disabled" value="320 chars left" style="width:100px; padding:5px; cursor:default;" /> <input type='submit' class='button' name="submitb" id="submitb" value="Send SMS">
    <
    input type="hidden" name="task" value="sendsms" />
    </
    form>

    {
    literal}
    <
    script>
    function 
    check_sms(){
        
    document.sms_form.chars_left.value 320 document.sms_form.sms_message.value.length;
    }
    </script>
    {/literal}

    {include file='admin_footer.tpl'} 

    and here is the coding in for the error :

    Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/username/public_html/testing/include/class_database.php on line 140

    here are the lines

    // THIS METHOD FETCHES A ROW AS AN ASSOCIATIVE ARRAY
    // INPUT: $result REPRESENTING A DATABASE QUERY RESULT RESOURCE
    // OUTPUT: RETURNS AN ASSOCIATIVE ARRAY FOR A DATABASE ROW
    function fetch_assoc($result) {

    return mysql_fetch_assoc($result); // This is the 140th line

    } // END fetch_assoc() METHOD

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Single most common error we see is an error in the use of the SQL resource.
    Generally on a mysql_fetch_* call, if it triggers this error it means you have a query that has returned an invalid pointer (your query has failed).

    The fix is easy, but the implementation will change. If this class_database is object oriented, I would throw an exception on a non-resource:
    PHP Code:
    function fetch_assoc($result)
    {
        return @
    mysql_fetch_assoc($result) or throw new Exception('Invalid query.');

    It would then require implementation:
    PHP Code:
    $result 'somequeriedpointer';
    try
    {
        
    $record $obj->fetch_assoc($result);
        
    // do something with a record
    }
    catch (
    Exception $e)
    {

    Alternatively, you can check if the actual query itself was successful with similar ideas. The problem all comes down to that $result provided to fetch_assoc is not a valid mysql resource pointer, and if it is from the result of a query, the query has failed and returned false.

    Edit:
    To expand, the most important part to get rid of the error is the suppressor (@) on the function. But DO NOT use suppressors unless you plan on dealing with the error (like how I threw an exception). This would also work, and is a bit more simplistic:
    PHP Code:
    function fetch_assoc($result)
    {
        return @
    mysql_fetch_assoc($result);
    }

    // Used with
    if ($record $obj->fetch_assoc($resource))
    {
        
    // Record is != false
        // do stuff with $record

    I'm big into OOP, so that is why I chose an exception toss.
    Last edited by Fou-Lu; 11-27-2010 at 03:28 PM.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    coolbud012 (11-27-2010)

  • #3
    New Coder
    Join Date
    Aug 2010
    Posts
    35
    Thanks
    2
    Thanked 0 Times in 0 Posts
    actually I am a total NOOB in php so can you please explain me more please if possible plz plz

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Quote Originally Posted by coolbud012 View Post
    actually I am a total NOOB in php so can you please explain me more please if possible plz plz
    Check my edit above, that should explain a bit more. You got in right when I was doing the edit.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    coolbud012 (11-27-2010)

  • #5
    New Coder
    Join Date
    Aug 2010
    Posts
    35
    Thanks
    2
    Thanked 0 Times in 0 Posts

    thanx

    yeah its working now bro...

  • #6
    New Coder
    Join Date
    Aug 2010
    Posts
    35
    Thanks
    2
    Thanked 0 Times in 0 Posts
    u r the best coder I have ever seen till now


  •  

    Posting Permissions

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