469,338 Members | 8,242 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,338 developers. It's quick & easy.

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?

Jul 17 '05 #1
15 4614
just thought i would add that i am using uk2.net as my hosting, and mysql
and php are setup fine.

TIA
Jul 17 '05 #2
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 =--
Jul 17 '05 #3
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
Jul 17 '05 #4
$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);)
Jul 17 '05 #5
removing the quotes from "SELECT id FROM stock"; didn't work either.
Jul 17 '05 #6
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 =--
Jul 17 '05 #7
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
Jul 17 '05 #8
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 =--
Jul 17 '05 #9
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.
Jul 17 '05 #10
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.
Jul 17 '05 #11
Matthew Robinson wrote:
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.


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 =--
Jul 17 '05 #12
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
Jul 17 '05 #13
sorry about the posting twice about the same thing again, but the line in
question is the one with while on it
Jul 17 '05 #14
Matthew Robinson wrote:
$qresult = mysql_query($command) or die(mysql_error());
$qresult here
while ($row = mysql_fetch_array($result)) {
but here you have $result
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


$result is *not* an object returned from mysql_query() function :)
--
--= my mail box only accepts =--
--= Content-Type: text/plain =--
--= Size below 10001 bytes =--
Jul 17 '05 #15
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.
Jul 17 '05 #16

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Robert | last post: by
5 posts views Thread by james | last post: by
15 posts views Thread by Good Man | last post: by
4 posts views Thread by Marcel Brekelmans | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by Purva khokhar | last post: by
1 post views Thread by haryvincent176 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.