Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • phpguy

    #1

    Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource

    Hi people im making an online booking system as a final year project in
    my university i get this error which i cannot seem to fix !

    Here is my code:

    PART 1 *************** *************** *************** ************

    #connect to sql
    $connect=@mysql _connect("local host","root","" )
    or die("Err:Conn") ;

    #database select

    $database=@mysq l_select_db("ti mes",$connect)
    or die("Err:Db");

    #create query


    //$sql = 'SELECT * FROM `show` ';

    $sql = "SELECT * FROM show WHERE from = '$from' AND to = '$to' ";



    #execute

    $query= @mysql_query($s ql,$database) or die('Error ' . mysql_error() .
    ' in ' . $sql);


    PART 2 *************** *************** *************** ************

    while ($row = mysql_fetch_arr ay($query))
    {

    $return="<font color=\"#FF0000 \"><b>&gt;&g t;</b></font> <a
    href=\"showall. php\">Select a RETURN travel </a><b><font
    color=\"#FF0000 \">&lt;&lt;</font></b><br>";
    $homepage="<fon t color=\"#FF0000 \"><b>&gt;&g t;</b></font> <a
    href=\"index.ph p\">Choose a route </a><b><font
    color=\"#FF0000 \">&lt;&lt;</font></b><br><br>";
    $changetravel=" <font color=\"#FF0000 \"><b>&gt;&g t;</b></font> <a
    href=\"bookexpi re.php\">CHANGE your travel</a> <b><font
    color=\"#FF0000 \">&lt;&lt;</font></b><br><br>";
    $checkout="<fon t color=\"#FF0000 \"><b>&gt;&g t;</b></font> <a
    href=\"book3.ph p\">CHECKOUT & PAYMENT</a> <b><font
    color=\"#FF0000 \">&lt;&lt;</font></b><br><br>";
    $oneway="<font color=\"#FF0000 \"><b>&gt;&g t;</b></font> <a
    href=\"showall. php\">Select a 1-WAY travel </a><b><font
    color=\"#FF0000 \">&lt;&lt;</font></b><br>";



    ?>

    <br>
    <br>
    <div align="center">
    <table border="1" bordercolordark ="#FF0000" cellspacing="0"
    cellpadding="0" bordercolorligh t="#FF0000" width="731">
    <tr>
    <td align="center"> <font
    color="#FF0000" ><u><b>TICKET TYPE</b></u></font></td>
    <td align="center"> <font
    color="#FF0000" ><u><b>FROM</b></u></font></td>
    <td align="center"> <font
    color="#FF0000" ><u><b>TO</b></u></font></td>
    <td align="center"> <font
    color="#FF0000" ><u><b>PRICE</b></u></font></td>
    <td align="center"> <font
    color="#FF0000" ><u><b>TIME</b></u></font></td>
    <td align="center"> <font
    color="#FF0000" ><u><b>DATE</b></u></font></td>
    </tr>
    <tr>
    <td align="center"> <?php echo $row["type"];
    ?></td>
    <td align="center"> <?php echo $row["from"];
    ?></td>
    <td align="center"> <?php echo $row["to"];
    ?></td>
    <td align="center"> <?php echo $row["price"]; ?>
    GBP</td>
    <td align="center"> <?php echo $row["time"];
    ?></td>
    <td align="center"> <?php echo $row["date"];
    ?></td>
    </tr>
    </table>
    <table border="1" width="435" bordercolordark ="#FFFF00"
    bordercolorligh t="#FFFF00" cellspacing="0" cellpadding="0" >
    <tr>





    *************** *************** *************** ************

    So whats wrong in my code ? This error only occurs when i want to
    select specific values form the database from a form (the $from,$to)
    values if i change my query to display ALL THE RESULTS using

    $sql = 'SELECT * FROM `show` ';
    itworks perfectly with no errors

    please help me !!

    cheers

  • phpguy

    #2
    Re: Warning: mysql_fetch_arr ay(): supplied argument is not a valid MySQL result resource

    Also if i place the @ symbol to while ($row =
    @mysql_fetch_ar ray($query)) i get a blank page and no result is
    displayed, normally results from the database should be displayed with
    criteria of departure and arrival. The while loop works great if the
    querty chanbages to simply display all the data of the database but
    when we become specific and we specify the field from of the database
    to be equal to the $from which is the user input from the form we get
    errors

    cheers

    Comment

    • claudel@gmail.com

      #3
      Re: Warning: mysql_fetch_arr ay(): supplied argument is not a valid MySQL result resource

      quite simple, and a very often mistake :)

      show is a keyword in sql - think of show tables; or show databases; so
      changing show to `show` in the actual query will work:
      $sql = "SELECT * FROM `show` WHERE from = '$from' AND to = '$to' ";

      Comment

      • phpguy

        #4
        Re: Warning: mysql_fetch_arr ay(): supplied argument is not a valid MySQL result resource

        thanks but still doesnt work. The error is said to be here


        while ($row = mysql_fetch_arr ay($query))

        man i have tried everything !!! Also i want to say it works if i
        include in the query specific values such as $sql = "SELECT * FROM
        `show` WHERE from = 'euston' "; it only messes up when i use variables
        !!!

        Comment

        • Ken Robinson

          #5
          Re: Warning: mysql_fetch_arr ay(): supplied argument is not a valid MySQL result resource


          phpguy wrote (in part):[color=blue]
          > Hi people im making an online booking system as a final year project[/color]
          in[color=blue]
          > my university i get this error which i cannot seem to fix !
          >
          > Here is my code:
          >
          > PART 1 *************** *************** *************** ************
          >
          > #connect to sql
          > $connect=@mysql _connect("local host","root","" )
          > or die("Err:Conn") ;
          >
          > #database select
          >
          > $database=@mysq l_select_db("ti mes",$connect)
          > or die("Err:Db");
          >
          > #create query
          >
          >
          > //$sql = 'SELECT * FROM `show` ';
          >
          > $sql = "SELECT * FROM show WHERE from = '$from' AND to = '$to' ";
          >
          >
          >
          > #execute
          >
          > $query= @mysql_query($s ql,$database) or die('Error ' . mysql_error()[/color]
          ..[color=blue]
          > ' in ' . $sql);
          >
          >
          > PART 2 *************** *************** *************** ************
          >
          > while ($row = mysql_fetch_arr ay($query))
          > {
          >[/color]

          [snip]
          [color=blue]
          > So whats wrong in my code ? This error only occurs when i want to
          > select specific values form the database from a form (the $from,$to)
          > values if i change my query to display ALL THE RESULTS using
          >
          > $sql = 'SELECT * FROM `show` ';
          > itworks perfectly with no errors[/color]

          Where do the varibles "$from" and "$to" come from? If they are coming
          from a submitted form or the URL, you should use $_POST['from'] for
          forms or $_GET['from'] for input on the URL.

          Also, print out your query just before your "mysql_quer y" statement.
          That should be able to help you find your error.

          Ken

          Comment

          • phpguy

            #6
            Re: Warning: mysql_fetch_arr ay(): supplied argument is not a valid MySQL result resource

            Hello Ken and thanks but still errors.

            $from and $to come from a form and thats what i did

            $from=$_POST['from'];
            $to=$_POST['to'];
            $type=$_POST['type'];

            also the sql stuff is between the HEADERS of my page while the while
            loop is in the body does this have anything to do with it ?

            cheers

            Comment

            • phpguy

              #7
              Re: Warning: mysql_fetch_arr ay(): supplied argument is not a valid MySQL result resource

              guys problem solved !!! thats the query that solved the problem

              $sql= "SELECT * FROM `show` WHERE `from` = '$from' AND `to` = '$to'
              ";

              thanks for everytihng

              Comment

              Working...