473,394 Members | 1,550 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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 4740
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Robert | last post by:
<?php echo "HI " . $name; $result = mysql_query("SELECT * FROM album WHERE username='{$name}'"); while ($next_row = mysql_fetch_array($result)) { $album_name = $next_row; echo "<a...
5
by: james | last post by:
I am new to PHP and am trying to run a simple query and display the result, with no luck. Here is the code I am using. <?php //start session session_start(); //store cmpid from querystring...
4
by: Konrad | last post by:
In the part of code: $polecenie = "SELECT osoby.Id ,osoby.Imie , osoby.Nazwisko,osoby.Tytul,osoby.Email,adrespraca.Adres AS ap, adrespraca.KodPoczt AS...
15
by: Good Man | last post by:
Hey there I have a dumb question.... Let's say i have a database full of 4000 people.... I select everything from the database by: $result = mysql_query("SELECT * FROM People");
3
by: Pratchaya | last post by:
Hi Everyone ============================================================== About PHP::: Error/Problem PHP Warning: mysql_fetch_array():...
4
by: Marcel Brekelmans | last post by:
Hello, I seem to get an extra empty field in every 'mysql_fetch_array' command I issue. For example: I have a simple table 'tblName': ID Name 1 Jane 2 Joe 2 Doe
9
by: Petr Vileta | last post by:
Hi, I'm new here and excuse me if this question was be here earlier. I have a simple code <html><body> <?php <?php $link = mysql_connect("localhost", "user", "password") or die("Grr: " ....
1
by: student2008 | last post by:
Sorry about the title its a tricky one. I have a form which allows me to add a question and answers into a mysql database via a combination of, if a certain option is chosen and the reset button...
11
by: chemlight | last post by:
I'm having a problem. I'm sure I'm going to kick myself over the answer... I have a table that stores vendors and their languages. This table starts out blank. I am querying the table to see if a...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.