mysql_fetch_array problem

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 ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 01-07-2004
Matthew Robinson
 
Posts: n/a
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?

Reply With Quote
  #2 (permalink)  
Old 01-07-2004
Matthew Robinson
 
Posts: n/a
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
Reply With Quote
  #3 (permalink)  
Old 01-07-2004
Pedro Graca
 
Posts: n/a
Default Re: mysql_fetch_array problem

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 =--
Reply With Quote
  #4 (permalink)  
Old 01-07-2004
Dan Tripp
 
Posts: n/a
Default Re: mysql_fetch_array problem

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
Reply With Quote
  #5 (permalink)  
Old 01-07-2004
Matthew Robinson
 
Posts: n/a
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: 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);)
Reply With Quote
  #6 (permalink)  
Old 01-07-2004
Pedro Graca
 
Posts: n/a
Default Re: mysql_fetch_array problem

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 =--
Reply With Quote
  #7 (permalink)  
Old 01-07-2004
Matthew Robinson
 
Posts: n/a
Default Re: mysql_fetch_array problem

removing the quotes from "SELECT id FROM stock"; didn't work either.
Reply With Quote
  #8 (permalink)  
Old 01-07-2004
Matthew Robinson
 
Posts: n/a
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
Reply With Quote
  #9 (permalink)  
Old 01-07-2004
Pedro Graca
 
Posts: n/a
Default Re: mysql_fetch_array problem

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 =--
Reply With Quote
  #10 (permalink)  
Old 01-07-2004
Matthew Robinson
 
Posts: n/a
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.
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 07:21 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0