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.
Page 1 of 2 12 LastLast
Results 1 to 15 of 20
  1. #1
    Senior Coder
    Join Date
    May 2006
    Posts
    1,673
    Thanks
    28
    Thanked 4 Times in 4 Posts

    Script works but get a warning: mysql_fetch_assoc() expects parameter 1 to be resourc

    Hi,

    I have written a script to build a queue for emails.

    It is working but I get a warning:

    Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in /home/exp526d/public_html/queue_create.php on line 164
    This is my code:

    PHP Code:
    $sql "SELECT broadcast_id, all_camps, camp_no, client_no, subject, message FROM broadcasts WHERE live = 'y' AND approve = 'y' AND queued = 'n' AND send_date <= now() ";
    $result mysql_query($sql)    or write_error("Could not do Broadcast Query 1 ".mysql_error()." \r\n");     
    $num mysql_num_rows($result);
    if (
    $num == ) {
        
    write_log("No Valid Broadcasts");
        } 
    // end if

    else {        
        echo 
    "<br>$sql<br>";
        while(
    $row mysql_fetch_assoc($result)){
        
    extract($row); 
    Line 164 is :
    while($row = mysql_fetch_assoc($result)){

    I guess my query contains an error but I just can not see where

    Does it contain some "reserved words" or something ?

    Any help, much appreciated.

    Thanks



    .
    If you want to attract and keep more clients, then offer great customer support.

    Support-Focus.com. automates the process and gives you a trust seal to place on your website.
    I recommend that you at least take the 30 day free trial.

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,801
    Thanks
    160
    Thanked 2,216 Times in 2,203 Posts
    Blog Entries
    1
    $result = mysql_query($sql) or write_error("Could not do Broadcast Query 1 ".mysql_error()." \r\n");
    What's write_error() ? What's its outcome?
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #3
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,092
    Thanks
    51
    Thanked 506 Times in 493 Posts
    Quote Originally Posted by jeddi View Post
    This is my code:

    [PHP]$sql = "SELECT broadcast_id, all_camps, camp_no, client_no, subject, message FROM broadcasts WHERE live = 'y' AND approve = 'y' AND queued = 'n' AND send_date <= now() ";
    @jeddi: The message means that mysql didn't return a mysql result resource but instead returned false - IE because it couldn't understand your SQL. Either that or write_error() is using SQL that is wrong.

    You need to run your SQL through phpmyadmin (click the table you want then the SQL tab at the top, paste it in, substitute PHP variables for real values and see what happens). You should get an error message from phpmyadmin indicating where the fault is in the SQL.
    My helpful sig is on vacation trying to loose some weight. It got a bit fat and caused a few problems but it will be back at some point!

  • #4
    Senior Coder
    Join Date
    May 2006
    Posts
    1,673
    Thanks
    28
    Thanked 4 Times in 4 Posts
    OK

    I got rid of the function write_error() and replaced it with a "die"

    So I now have:

    PHP Code:
    $sql "SELECT camp_id, camps.client_no, author, mess_no, next_camp, sub_id, sub_name, sub_email, country, em_type FROM camps, subscriptions WHERE live = 'y' AND approve = 'y' AND confirm = 'y' AND queued = 'n' AND mess_no > '0' AND subscriptions.camp_no = camps.camp_id AND send_date <= now() ";

    echo 
    "<br>$sql<br>";

    $result mysql_query($sql) or die("Could not do emailqueue 2: ".mysql_error()); 
    $num mysql_num_rows($result);

    echo 
    "<br>$num<br>";

    if (
    $num == ) {
        
    write_log("No Valid Campaigns");
        } 
    // end if

    else {    
        
        while(
    $row mysql_fetch_assoc($result)){
        
    extract($row); 

    And I get this output:

    SELECT camp_id, camps.client_no, author, mess_no, next_camp, sub_id, sub_name, sub_email, country, em_type FROM camps, subscriptions WHERE live = 'y' AND approve = 'y' AND confirm = 'y' AND queued = 'n' AND mess_no > '0' AND subscriptions.camp_no = camps.camp_id AND send_date <= now()

    3
    Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in /home/exp526d/public_html/queue_create.php on line 46
    Line 46:
    while($row = mysql_fetch_assoc($result)){


    So it finds 3 valid results but I get the warning !
    Doesn't make sense .. or does it. ??


    It appears that the query is OK because it doesn't Die.
    I don't get "Could not do emailqueue 2: " and the mysql_error !!

    So if there's no error ... why the warning ??


    .
    Last edited by jeddi; 04-13-2012 at 01:58 PM.
    If you want to attract and keep more clients, then offer great customer support.

    Support-Focus.com. automates the process and gives you a trust seal to place on your website.
    I recommend that you at least take the 30 day free trial.

  • #5
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,092
    Thanks
    51
    Thanked 506 Times in 493 Posts
    I personally never bother using 'or die()' in my scripts. I found it a bit tempermental. Instead I ise if/else. If i remember correctly, or die() only works with mysql_connect and mysql_select_db(). I have a vague memory of helping another person via TeamViewer with similar problems to this and I removed the or die() from his call to mysql_query().

    The reason you are still getting this error ie because your mysql_query() call is returning a false result and it's passing that to mysql_fetch_array() which expects a resource.

    There IS something wrong there somewhere but without seeing it in action it's difficult to advise more.

    I want you to change this:
    PHP Code:
    $result mysql_query($sql) or die("Could not do emailqueue 2: ".mysql_error()); 
    to this:
    PHP Code:
    if ($result mysql_query($sql))
       {
       print 
    mysql_num_rows($result);
       }
    else
       {
       print 
    mysql_error();
       } 
    Report back with your findings.
    My helpful sig is on vacation trying to loose some weight. It got a bit fat and caused a few problems but it will be back at some point!

  • #6
    Senior Coder
    Join Date
    May 2006
    Posts
    1,673
    Thanks
    28
    Thanked 4 Times in 4 Posts
    OK I did what you suggested:


    PHP Code:
    $sql "SELECT camp_id, camps.client_no, author, mess_no, next_camp, sub_id, sub_name, sub_email, country, em_type FROM camps, subscriptions WHERE live = 'y' AND approve = 'y' AND confirm = 'y' AND queued = 'n' AND mess_no > '0' AND subscriptions.camp_no = camps.camp_id AND send_date <= now() ";

    echo 
    "<br>$sql<br>";

    if (
    $result mysql_query($sql))
       {
       print 
    mysql_num_rows($result);
       }
    else
       {
       print 
    mysql_error();
       }  
       
    $num mysql_num_rows($result);

    echo 
    "<br>$num<br>";

    if (
    $num == ) {
        
    write_log("No Valid Campaigns");
        } 
    // end if

    else {    
        
        while(
    $row mysql_fetch_assoc($result)){
        
    extract($row); 
    SELECT camp_id, camps.client_no, author, mess_no, next_camp, sub_id, sub_name, sub_email, country, em_type FROM camps, subscriptions WHERE live = 'y' AND approve = 'y' AND confirm = 'y' AND queued = 'n' AND mess_no > '0' AND subscriptions.camp_no = camps.camp_id AND send_date <= now()
    5
    5
    Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in /home/exp526d/public_html/queue_create.php on line 54
    And 54 is:
    while($row = mysql_fetch_assoc($result)){

    Does this help ?

    Thanks again for helping.


    .
    If you want to attract and keep more clients, then offer great customer support.

    Support-Focus.com. automates the process and gives you a trust seal to place on your website.
    I recommend that you at least take the 30 day free trial.

  • #7
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,092
    Thanks
    51
    Thanked 506 Times in 493 Posts
    Yes / No. It helps as it's showing that something really screwy is going on but it doesn't help because its not executing the code block I'd expect (the print mysql_error() bit).

    Do you have Teamviewer so I can connect to your desktop and see this for myself? If you do, please PM me your connection details - I WILL get to the bottom of this.
    My helpful sig is on vacation trying to loose some weight. It got a bit fat and caused a few problems but it will be back at some point!

  • #8
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,092
    Thanks
    51
    Thanked 506 Times in 493 Posts
    Ok, well an hour has gone now so you'll have to arrange a time with me by PM instead.
    My helpful sig is on vacation trying to loose some weight. It got a bit fat and caused a few problems but it will be back at some point!

  • #9
    Senior Coder
    Join Date
    May 2006
    Posts
    1,673
    Thanks
    28
    Thanked 4 Times in 4 Posts
    Hi - no I don't have Teamviewer


    I just thought I'd add something extra:

    I added: echo "<br>$result<br>";

    PHP Code:
    $sql "SELECT camp_id, camps.client_no, author, mess_no, next_camp, sub_id, sub_name, sub_email, country, em_type FROM camps, subscriptions WHERE live = 'y' AND approve = 'y' AND confirm = 'y' AND queued = 'n' AND mess_no > '0' AND subscriptions.camp_no = camps.camp_id AND send_date <= now() ";

    echo 
    "<br>$sql<br>";

    if (
    $result mysql_query($sql))
       {
       print 
    mysql_num_rows($result);
       }
    else
       {
       print 
    mysql_error();
       }  
       
    echo 
    "<br>$result<br>";   
     
    $num mysql_num_rows($result);

    echo 
    "<br>$num<br>";

    if (
    $num == ) {
        
    write_log("No Valid Campaigns");
        } 
    // end if

    else {    
        
        while(
    $row mysql_fetch_assoc($result)){
        
    extract($row); 

    The output:


    SELECT camp_id, camps.client_no, author, mess_no, next_camp, sub_id, sub_name, sub_email, country, em_type FROM camps, subscriptions WHERE live = 'y' AND approve = 'y' AND confirm = 'y' AND queued = 'n' AND mess_no > '0' AND subscriptions.camp_no = camps.camp_id AND send_date <= now()
    5
    Resource id #5

    5
    Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in /home/exp526d/public_html/queue_create.php on line 56
    So $result DOES contain a Resource !!!

    So whats this stupid warning about then ??


    Any know why this is happening ?


    Thanks




    .
    If you want to attract and keep more clients, then offer great customer support.

    Support-Focus.com. automates the process and gives you a trust seal to place on your website.
    I recommend that you at least take the 30 day free trial.

  • #10
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,980
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    PHP Code:
        $i 0;
        while(++
    $i && ($row mysql_fetch_assoc($result))){
        
    printf("Iteration: %d" PHP_EOL$i);
        
    extract($row); 
    Does that alter the output?

  • #11
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,478
    Thanks
    0
    Thanked 635 Times in 625 Posts
    Can you show the complete loop that starts with

    PHP Code:
    while($row mysql_fetch_assoc($result)){ 
        
    extract($row); 
    It has to be something later in that loop that is changing the value of $result so that the second (or 5th) time that it returns to test the while condition the $result field no longer contains the resource.

    The tests you have already done prove it contains a resource the first time it is tested but you haven't shown all the code it runs before the second test.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #12
    Regular Coder
    Join Date
    Apr 2004
    Posts
    298
    Thanks
    0
    Thanked 23 Times in 23 Posts
    Re: extract, see http://php.net/manual/en/function.extract.php the section by darylblake at gmail dot com 12-Apr-2011 05:07

  • #13
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,478
    Thanks
    0
    Thanked 635 Times in 625 Posts
    Quote Originally Posted by litebearer View Post
    Re: extract, see http://php.net/manual/en/function.extract.php the section by darylblake at gmail dot com 12-Apr-2011 05:07
    Obviously you have a typo somewhere - we need to see your actual code not the code you have miscopied.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #14
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,092
    Thanks
    51
    Thanked 506 Times in 493 Posts
    Seems the op has a preference for going quiet on us now the weekend is here.

    The op also hasn't mentioned if its the first loop thats causing this or the 2nd/3rd etc which is rather important.
    My helpful sig is on vacation trying to loose some weight. It got a bit fat and caused a few problems but it will be back at some point!

  • #15
    Senior Coder
    Join Date
    May 2006
    Posts
    1,673
    Thanks
    28
    Thanked 4 Times in 4 Posts
    Hello,

    Thanks for all the replies.

    My "going quite" is due to needing to sleep occasionally

    We are in different time zones. I am presently in Turkey !

    Now - some great points made about the value of $result
    possibly changing etc.

    I'll take a look and report back.

    Thanks again
    If you want to attract and keep more clients, then offer great customer support.

    Support-Focus.com. automates the process and gives you a trust seal to place on your website.
    I recommend that you at least take the 30 day free trial.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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