Professional Web Applications Themes

mysql_fetch_array problem - PHP Development

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("SELECT id FROM 'stock'"); echo ("START<P><HR>"); while ($row = mysql_fetch_array($qresult)) { echo ($row ["name"]); echo ("<br>"); } echo ("<P><HR><P>END"); OUTPUT: START Warning: mysql_fetch_array(): 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?...

  1. #1

    Default 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("SELECT id FROM 'stock'");
    echo ("START<P><HR>");
    while ($row = mysql_fetch_array($qresult)) {
    echo ($row ["name"]);
    echo ("<br>");
    }
    echo ("<P><HR><P>END");

    OUTPUT:
    START
    Warning: mysql_fetch_array(): 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 Guest

  2. #2

    Default Re: mysql_fetch_array problem

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

    TIA
    Matthew Guest

  3. #3

    Default Re: mysql_fetch_array problem

    Matthew Robinson wrote: 

    Use error checking!


    Instead of

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

    do

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

    or, easier to interpret the (eventual) error

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

  4. #4

    Default Re: mysql_fetch_array problem

    Matthew Robinson wrote: 


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

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

    Regards,

    - Dan
    dantripp.com
    Dan Guest

  5. #5

    Default Re: mysql_fetch_array problem

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

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


    gives this error: P error: p 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($command) or die('Error ' mysql_error() ' in ' $command);)
    Matthew Guest

  6. #6

    Default Re: mysql_fetch_array problem

    Matthew Robinson wrote: 
     


    You lack the dots to concatenate the strings.

    $qresult = mysql_query($command) 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 =--
    Pedro Guest

  7. #7

    Default Re: mysql_fetch_array problem

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

  8. #8

    Default Re: mysql_fetch_array 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 'houseproudlancs_co_uk1' database.

    <?php

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

    $select = mysql_select_db("houseproudlancs_co_uk1");


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

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

    ?>



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


    --------------------------------
    END
    Matthew Guest

  9. #9

    Default Re: mysql_fetch_array problem

    Matthew Robinson wrote: 

    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"
     
    empty, of course :) 
    --
    --= my mail box only accepts =--
    --= Content-Type: text/plain =--
    --= Size below 10001 bytes =--
    Pedro Guest

  10. #10

    Default Re: mysql_fetch_array 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.
    Matthew Guest

  11. #11

    Default Re: mysql_fetch_array problem

    Matthew Robinson wrote: 

    When you do

    $sql = 'select col1, col2, col4 from table';
    $obj = mysql_query($sql) or die(mysql_error());
    $res=mysql_fetch_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 =--
    Pedro Guest

  12. #12

    Default Re: mysql_fetch_array 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.
    Matthew Guest

  13. #13

    Default Re: mysql_fetch_array problem

    CODE:

    <?php

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


    $select = mysql_select_db("houseproudlancs_co_uk1");





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

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

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

    ?>


    OUTPUT:

    START


    Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/houseproudlancs_co_uk/index_to_be.php on line 17


    END
    Matthew Guest

  14. #14

    Default Re: mysql_fetch_array problem

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

  15. #15

    Default Re: mysql_fetch_array problem

    Matthew Robinson wrote: 

    $qresult here
     

    but here you have $result


     

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

  16. #16

    Default Re: mysql_fetch_array problem

    thanks for the help pedro, im not used to programming properly, as i used
    to program vb, which wipes your bum for you. anyway, thanks again. this
    has been a problem for days.
    Matthew Guest

Similar Threads

  1. resetting mysql_fetch_array()
    By jg in forum PHP Development
    Replies: 1
    Last Post: April 5th, 07:19 PM
  2. problems with mysql_fetch_array in 2D array
    By Kevin Torr in forum PHP Development
    Replies: 6
    Last Post: February 13th, 10:27 PM
  3. mysql_fetch_array
    By Robert in forum PHP Development
    Replies: 8
    Last Post: January 7th, 08:09 AM
  4. Replies: 0
    Last Post: October 27th, 11:36 AM
  5. #25998 [NEW]: mysql_fetch_array display nothing over 530 records
    By hsutanto78 at hotmail dot com in forum PHP Development
    Replies: 0
    Last Post: October 27th, 09:17 AM

Bookmarks

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139