...

View Full Version : Please please help me



coolbud012
11-27-2010, 03:12 PM
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..

{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 (comma, seperated):<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

Fou-Lu
11-27-2010, 03:20 PM
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:


function fetch_assoc($result)
{
return @mysql_fetch_assoc($result) or throw new Exception('Invalid query.');
}

It would then require implementation:


$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.


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:


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.

coolbud012
11-27-2010, 03:28 PM
actually I am a total NOOB in php so can you please explain me more please if possible plz plz

Fou-Lu
11-27-2010, 03:29 PM
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.

coolbud012
11-27-2010, 04:42 PM
yeah its working now bro...

coolbud012
11-27-2010, 04:43 PM
u r the best coder I have ever seen till now



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum