• PHP Help PHP Newbies
  • Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource

thank you for the quick reply, i have no idea what happened, but its seemed to of fixed itself, however i am getting a different error,

Parse error: syntax error, unexpected T_WHILE in..... on this line of code

while($r = mysql_fetch_array($pull_dates)){

    Check the line of code beforehand. You are probably missing a semi-colon or closing item like a quote or parenthesis.

      you were right i missed a semi colon out, it did bring up a third error though, im sorry bout all this.

      fatal error, call to undifined function: mysql_last_error.....

      this was on the line of code that i just added.... $error = mysql_last_error($pull_dates);

      i know what its saying but do not know how to rectify it.

      thank you for any help with this

      18241

        unfourtunately thats just bought up the same error on that line 😛 , it has to be something im doing wrong i just cant see what, if you cant tell im not a very big programmer.

        heres the whole code as it stands

        <?php

        $db = mysql_connect("localhost", "user", "passsword");

        mysql_select_db("database",$db);

        $date1 = $POST['date1'];
        $date2 = $
        POST['date2'];

        $date1 = explode("/",$date1);

        year = 2

        $date2 = explode("/",$date2);

        $date1= "$date1[2]-$date1[1]-$date1[0]";
        $date2= "$date2[2]-$date2[1]-$date2[0]";

        $sql = "SELECT * FROM calls WHERE date => '$date1' AND <='$date2'";

        $pull_dates = mysql_query($sql);
        $error = mysql_error($pull_dates);

        while($r = mysql_fetch_array($pull_dates)){

        $call_call = $r['called'];
        $call_comp= $r['company_called'];
        $call_leng= $r['length_of_call'];
        $call_desc= $r['description_of_call'];
        $call_main= $r['person_who_called'];

        echo $call_call;
        echo $call_comp;
        echo $call_leng;
        echo $call_desc;
        echo $call_main;
        }
        echo "$error"
        ?>

          You are passing in query result. Remove the argument:

          $error = mysql_error();

            this takes it back to the original error, ive tried just adding numbers into this form that it relates to and this brings up the same error.

              Well, back to the original solution then :-)

              Take a look here:

              $date1= "$date1[2]-$date1[1]-$date1[0]";
              $date2= "$date2[2]-$date2[1]-$date2[0]";

              $sql = "SELECT * FROM calls WHERE date => '$date1' AND <='$date2'";

              This will expand to something like:

              $sql = "SELECT * FROM calls WHERE date => '00-00-00' AND <= '00-00-00'";

              Or similar. When I run this in an arbitrary table I get an invalid sql error. This is because you are comparing strings using greater-than and less-than.

              If you are going to use >= and <= then you need to make sure that $date1 and $date2 are actual numbers, not a string date. The alternative is to use the date functions in mysql OR even better, use the php date functions like http://us2.php.net/mktime

              $date1 = mktime(0, 0, 0, $date1[0], $date1[1], $date1[2]);
              $date2 = mktime(0, 0, 0, $date2[0], $date2[1], $date2[2]); 
              
              // Would produce something remotely close to this
              $sql = "SELECT * FROM calls WHERE date => '1936516186' AND <= '1951354968'"; 
              

              Which makes more sense for comparison.

              EDIT: also see http://us2.php.net/manual/en/function.strtotime.php

                zabmilenko wrote:

                If you are going to use >= and <= then you need to make sure that $date1 and $date2 are actual numbers, not a string date.

                Not true -- MySQL can compare dates as strings when in the format of 'YYYY-MM-DD'. Now, the query does have to be formed correctly:

                SELECT * FROM calls WHERE date => '$date1' AND date <='$date2'

                Also -- what column type is this "date" column?

                The first thing you need to do is to echo out $sql when an error occurs. This is how I normally debug SQL queries:

                $sql = "SELECT * FROM calls WHERE date => '$date1' AND <='$date2'"; 
                
                $pull_dates = mysql_query($sql) or die('MySQL Error: ' . mysql_error() . '<hr>Query: ' . $sql);

                If you see a MySQL error message, reply with the exact error message & the outputted MySQL query underneath it.

                If you don't receive a MySQL error, but the script is still not running properly, echo out the $sql variable and post the query here for us to examine.

                  the date column is a date type, it did come up with an error.

                  MySQL Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '=> '2006-10-03' AND <='2006-10-03'' at line 1

                  that is the error. and this is what was underneath....

                  Query: SELECT * FROM calls WHERE date => '2006-10-03' AND <='2006-10-03'

                  18241

                    Try implementing my suggested fix:

                    bradgrafelman wrote:

                    Now, the query does have to be formed correctly:

                    SELECT * FROM calls WHERE date => '$date1' AND date <='$date2'

                      I don't think "=>" is a valid comparison operator. Try using ">=" instead.

                        i tried using the suggested fix and changed the comparison operator all to no avail unfourtunately, it is still coming up with the same error as before.....

                        Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /.../.../.../.../.../.../.../... on line 59
                        line 59 is the while function...

                        <?php
                        while($r = mysql_fetch_array($pull_dates));
                        ?>

                        $pull_dates refrences this

                        <?php
                        $sql = "SELECT * FROM calls WHERE called >=$date1 AND <= $date2";

                        $pull_dates = mysql_query($sql); i seriously cant see why it is falling over.
                        ?>

                          put a print statement to print the sql query and let's see whats wrong..

                            bradgrafelman wrote:

                            Not true -- MySQL can compare dates as strings when in the format of 'YYYY-MM-DD'. Now, the query does have to be formed correctly:

                            I didn't know that. Thank you.

                            As for the current query attempt: SELECT * FROM calls WHERE called >=$date1 AND <= $date2

                            Doesn't the dates need to be single-quoted at least?

                              zabmilenko wrote:

                              Doesn't the dates need to be single-quoted at least?

                              Yes, the dates do have to be single-quoted, as they are strings, not to mention the fact that the query is missing a column declaration in the second part of the comparison.

                              I'll say it again... perhaps I wasn't making it obvious enough last time...

                              SELECT * FROM calls WHERE called >= '$date1' AND called <= '$date2'

                                5 days later
                                18241 wrote:

                                Help please!!?

                                Im getting this warning on one of my pages and have no idea whats causing it.

                                here's the code.

                                <?php

                                $date1 = $POST['date1'];
                                $date2 = $
                                POST['date2'];

                                $date1 = explode("/",$date1);

                                $date2 = explode("/",$date2);

                                $date1= "$date1[2]-$date1[1]-$date1[0]";
                                $date2= "$date2[2]-$date2[1]-$date2[0]";

                                $sql = "SELECT * FROM calls WHERE date => '$date1' AND <='$date2'";

                                $pull_dates = mysql_query($sql);

                                while($r = mysql_fetch_array($pull_dates)){ //line where the error is appearing.

                                $call_call = $r['called'];
                                $call_comp= $r['company_called'];
                                $call_leng= $r['length_of_call'];
                                $call_desc= $r['description_of_call'];
                                $call_main= $r['person_who_called'];

                                echo $call_call;
                                echo $call_comp;
                                echo $call_leng;
                                echo $call_desc;
                                echo $call_main;
                                }//close the loop

                                ?>

                                any help with this would be greatly appreciated.

                                thank you in advance

                                18241

                                when i read this script, there is incomplete code in $pull_dates = mysql_query($sql); there is no $link_identifier("host","username","password").....so the query is not executed.....

                                  coekrix wrote:

                                  when i read this script, there is incomplete code in $pull_dates = mysql_query($sql); there is no $link_identifier = mysql_connect("host","username","password").....so the query is not executed.....

                                  iam sorry, i made a little mistake

                                    a year later

                                    Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\mysql_test.php on line 15

                                    <?php

                                    Define MySQL Settings

                                    define("MYSQL_HOST", "localhost");
                                    define("MYSQL_USER", "root");
                                    define("MYSQL_PASS", "password");
                                    define("MYSQL_DB", "test");

                                    $conn = mysql_connect("".MYSQL_HOST."", "".MYSQL_USER."", "".BLUE22."") or die(mysql_error());
                                    mysql_select_db("".MYSQL_DB."",$conn) or die(mysql_error());

                                    $sql = "SELECT * FROM test";
                                    $res = mysql_query($sql);

                                    while ($field = mysql_fetch_array($res)) //line where the error is appearing
                                    {
                                    $id = $field['id'];
                                    $name = $field['name'];

                                    echo 'ID: ' . $field['id'] . '<br />';
                                    echo 'Name: ' . $field['name'] . '<br /><br />';
                                    }

                                    ?>

                                    any help with this would be greatly appreciated.

                                    thank you in advance

                                      1. Check that there is a connection to the database server.
                                      2. Check that the correct database is selected.
                                      3. Check that the relevant query is correct.

                                      Incidentally:

                                      $conn = mysql_connect("".MYSQL_HOST."", "".MYSQL_USER."", "".BLUE22."") or die(mysql_error());
                                      mysql_select_db("".MYSQL_DB."",$conn) or die(mysql_error());

                                      should be:

                                      $conn = mysql_connect(MYSQL_HOST, MYSQL_USER, 'BLUE22') or die(mysql_error());
                                      mysql_select_db(MYSQL_DB, $conn) or die(mysql_error());

                                      I am wary of such defined constants though, particularly when they start with the same prefix as the name of a well known extension.

                                        Write a Reply...