This is a discussion on mysql_fetch_array problem within the PHP Language forums, part of the PHP Programming Forums category; I set up phpmyadmin, and it works fine, but my code isn't working. id is the correct column (it ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
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 Robinson wrote:
> 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? 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 =-- |
|
|||
|
Matthew Robinson wrote:
> 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? > 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 |
|
|||
|
$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: 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($command) or die('Error ' mysql_error() ' in ' $command);) |
|
|||
|
Matthew Robinson wrote:
> $qresult = mysql_query($command) or die('Error ' mysql_error() ' in ' > $command); //$qresult = mysql_query("SELECT id FROM stock"); echo > ("START<P><HR>"); > gives this error: Parse error: parse error in > /home/houseproudlancs_co_uk/index_to_be.php on line 12 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 =-- |
|
|||
|
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 Robinson wrote:
><?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"]); 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" > START > -------------------------------- empty, of course :) > -------------------------------- > END -- --= my mail box only accepts =-- --= Content-Type: text/plain =-- --= Size below 10001 bytes =-- |