mysql_fetch_array problem

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

    mysql_fetch_array problem

    I set up phpmyadmin, and it works fine, but my code isn't working. id is
    the correct column (it is the primary key), stock is the correct database.

    CODE:
    $qresult = mysql_query("SE LECT id FROM 'stock'");
    echo ("START<P><HR>" );
    while ($row = mysql_fetch_arr ay($qresult)) {
    echo ($row ["name"]);
    echo ("<br>");
    }
    echo ("<P><HR><P>END ");

    OUTPUT:
    START
    Warning: mysql_fetch_arr ay(): supplied argument is not a valid MySQL
    result resource in /home/houseproudlancs _co_uk/index_to_be.php on line 14
    END

    does anybody have any idea's why?

  • Matthew Robinson

    #2
    Re: mysql_fetch_arr ay problem

    just thought i would add that i am using uk2.net as my hosting, and mysql
    and php are setup fine.

    TIA

    Comment

    • Pedro Graca

      #3
      Re: mysql_fetch_arr ay problem

      Matthew Robinson wrote:[color=blue]
      > OUTPUT:
      > START
      > Warning: mysql_fetch_arr ay(): supplied argument is not a valid MySQL
      > result resource in /home/houseproudlancs _co_uk/index_to_be.php on line 14
      > END
      >
      > does anybody have any idea's why?[/color]

      Use error checking!


      Instead of

      <?php
      $qresult = mysql_query("se lect ...");
      ?>

      do

      <?php
      $qresult = mysql_query("se lect ...") or die(mysql_error ());
      ?>

      or, easier to interpret the (eventual) error

      <?php
      $command = 'select ...';
      $qresult = mysql_query($co mmand) or die('Error ' . mysql_error() .
      ' in ' . $command);
      ?>
      --
      --= my mail box only accepts =--
      --= Content-Type: text/plain =--
      --= Size below 10001 bytes =--

      Comment

      • Dan Tripp

        #4
        Re: mysql_fetch_arr ay problem

        Matthew Robinson wrote:[color=blue]
        > I set up phpmyadmin, and it works fine, but my code isn't working. id is
        > the correct column (it is the primary key), stock is the correct database.
        >
        > CODE:
        > $qresult = mysql_query("SE LECT id FROM 'stock'");
        > echo ("START<P><HR>" );
        > while ($row = mysql_fetch_arr ay($qresult)) {
        > echo ($row ["name"]);
        > echo ("<br>");
        > }
        > echo ("<P><HR><P>END ");
        >
        > OUTPUT:
        > START
        > Warning: mysql_fetch_arr ay(): supplied argument is not a valid MySQL
        > result resource in /home/houseproudlancs _co_uk/index_to_be.php on line 14
        > END
        >
        > does anybody have any idea's why?
        >[/color]


        Omit the single quotes from your query, like so:
        $qresult = mysql_query("SE LECT id FROM stock");

        Putting single quotes around the table name causes a SQL error.

        Regards,

        - Dan
        dantripp.com

        Comment

        • Matthew Robinson

          #5
          Re: mysql_fetch_arr ay problem

          $command = "SELECT id FROM stock";
          $qresult = mysql_query($co mmand) or die('Error ' mysql_error() ' in '
          $command); //$qresult = mysql_query("SE LECT id FROM stock"); echo
          ("START<P><HR>" );

          while ($row = mysql_fetch_arr ay($qresult)) {
          echo ($row ["name"]);
          echo ("<br>");
          }
          echo ("<P><HR><P>END ");


          gives this error: Parse error: parse error in
          /home/houseproudlancs _co_uk/index_to_be.php on line 12

          line 12 in the page is the second line on this post ( $qresult =
          mysql_query($co mmand) or die('Error ' mysql_error() ' in ' $command);)

          Comment

          • Matthew Robinson

            #6
            Re: mysql_fetch_arr ay problem

            removing the quotes from "SELECT id FROM stock"; didn't work either.

            Comment

            • Pedro Graca

              #7
              Re: mysql_fetch_arr ay problem

              Matthew Robinson wrote:[color=blue]
              > $qresult = mysql_query($co mmand) or die('Error ' mysql_error() ' in '
              > $command); //$qresult = mysql_query("SE LECT id FROM stock"); echo
              > ("START<P><HR>" );[/color]
              [color=blue]
              > gives this error: Parse error: parse error in
              > /home/houseproudlancs _co_uk/index_to_be.php on line 12[/color]


              You lack the dots to concatenate the strings.

              $qresult = mysql_query($co mmand) or die('Error ' . mysql_error() . ' in ' . $command);
              // _______________ _______________ _______________ _^_____________ __^________^___ ________

              // sorry for the long lines :)
              --
              --= my mail box only accepts =--
              --= Content-Type: text/plain =--
              --= Size below 10001 bytes =--

              Comment

              • Matthew Robinson

                #8
                Re: mysql_fetch_arr ay problem

                thanks - it works now, well that bit anyway. here's the entire page, and
                the output below it. im trying to work out php/mysql, so im just trying to
                get it to print out all the fields in the 'name' field in the 'stock'
                table in the 'houseproudlanc s_co_uk1' database.

                <?php

                $dbcnx = @mysql_connect( "server", "username", "password") ;

                $select = @mysql_select_d b("houseproudla ncs_co_uk1");


                $command = "SELECT id FROM stock";
                $qresult = mysql_query($co mmand) or die('Error ' . mysql_error() . ' in ' . $command); echo ("START<P><HR>" );

                while ($row = mysql_fetch_arr ay($qresult)) {
                echo ($row ["name"]);
                echo ("<br>");
                }
                echo ("<P><HR><P>END ");

                ?>



                START
                --------------------------------


                --------------------------------
                END

                Comment

                • Pedro Graca

                  #9
                  Re: mysql_fetch_arr ay problem

                  Matthew Robinson wrote:[color=blue]
                  ><?php
                  > $dbcnx = @mysql_connect( "server", "username", "password") ;
                  > $select = @mysql_select_d b("houseproudla ncs_co_uk1");
                  >
                  > $command = "SELECT id FROM stock";
                  > $qresult = mysql_query($co mmand) or die('Error ' . mysql_error() . ' in ' . $command);
                  > echo ("START<P><HR>" );
                  >
                  > while ($row = mysql_fetch_arr ay($qresult)) {
                  > echo ($row ["name"]);[/color]

                  Do you want the "name" or the "id"?

                  At this point $row['name'] -- I prefer single quotes :) -- does not exist.

                  Either also select the name: "SELECT id, name FROM stock"
                  or change id's identification: "SELECT id as name FROM stock"
                  [color=blue]
                  > START
                  > --------------------------------[/color]
                  empty, of course :)[color=blue]
                  > --------------------------------
                  > END[/color]
                  --
                  --= my mail box only accepts =--
                  --= Content-Type: text/plain =--
                  --= Size below 10001 bytes =--

                  Comment

                  • Matthew Robinson

                    #10
                    Re: mysql_fetch_arr ay problem

                    oh ye, just realised i didn't mention that the database has 2 rows, both
                    with the 'name' column not null, so the output should be different to what
                    it is.

                    Comment

                    • Matthew Robinson

                      #11
                      Re: mysql_fetch_arr ay problem

                      id is the primary key, so i thought it would be best to use that as the
                      one to identify the row.

                      name is what i want to print to the screen.

                      Comment

                      • Pedro Graca

                        #12
                        Re: mysql_fetch_arr ay problem

                        Matthew Robinson wrote:[color=blue]
                        > oh ye, just realised i didn't mention that the database has 2 rows, both
                        > with the 'name' column not null, so the output should be different to what
                        > it is.[/color]

                        When you do

                        $sql = 'select col1, col2, col4 from table';
                        $obj = mysql_query($sq l) or die(mysql_error ());
                        $res=mysql_fetc h_array($obj);

                        // $res as as many elements as columns in your select, and their
                        // index is the name used in the select.

                        // so, now you can do
                        echo $res['col1'], $res['col2'], $res['col4'];

                        // but
                        echo $res['col3'];
                        // does not work even if the table has a column named "col3"
                        --
                        --= my mail box only accepts =--
                        --= Content-Type: text/plain =--
                        --= Size below 10001 bytes =--

                        Comment

                        • Matthew Robinson

                          #13
                          Re: mysql_fetch_arr ay problem

                          CODE:

                          <?php

                          $dbcnx = @mysql_connect( "SERV", "USR", "PWD");


                          $select = @mysql_select_d b("houseproudla ncs_co_uk1");





                          echo ("START<P><HR>" );

                          $command = 'SELECT id, name, description, price FROM stock';
                          $qresult = mysql_query($co mmand) or die(mysql_error ());

                          while ($row = mysql_fetch_arr ay($result)) {
                          echo ($row ["name"]);
                          echo ("<br>");
                          }
                          echo ("<P><HR><P>END ");

                          ?>


                          OUTPUT:

                          START


                          Warning: mysql_fetch_arr ay(): supplied argument is not a valid MySQL result resource in /home/houseproudlancs _co_uk/index_to_be.php on line 17


                          END

                          Comment

                          • Matthew Robinson

                            #14
                            Re: mysql_fetch_arr ay problem

                            sorry about the posting twice about the same thing again, but the line in
                            question is the one with while on it

                            Comment

                            • Pedro Graca

                              #15
                              Re: mysql_fetch_arr ay problem

                              Matthew Robinson wrote:[color=blue]
                              > $qresult = mysql_query($co mmand) or die(mysql_error ());[/color]

                              $qresult here
                              [color=blue]
                              > while ($row = mysql_fetch_arr ay($result)) {[/color]

                              but here you have $result


                              [color=blue]
                              > Warning: mysql_fetch_arr ay(): supplied argument is not a valid MySQL result resource in /home/houseproudlancs _co_uk/index_to_be.php on line 17[/color]

                              $result is *not* an object returned from mysql_query() function :)
                              --
                              --= my mail box only accepts =--
                              --= Content-Type: text/plain =--
                              --= Size below 10001 bytes =--

                              Comment

                              Working...