Solved

Query Selection Help

Posted on 2003-12-08
5
246 Views
Last Modified: 2008-03-06
I am trying to display the data in my database but I get the error below:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/anudaysp/public_html/displayServices.php on line 19



Below is my code:

#CONNECT TO MYSQL
$dbcnx = @mysql_connect('localhost',$username,$password) or die("Unable to connect to MySQL");

#CONNECT TO DATABASE
@mysql_select_db($database) or die( "Unable to select database");

#CREATE THE QUERY
$sql = "SELECT S_NAME,SERVICE FROM SERVICE";

#EXECUTE QUERY
$rs = mysql_query($sql,$dbcnx);

#Write the data
while($row = mysql_fetch_array($rs)) <- Line 19
{
      echo("Services: " .$row["s_name"]);
}



Also, What's the best way to debug in PHP?  Believe it or not, today is my second day coding in PHP.
0
Comment
Question by:Isaac
  • 2
  • 2
5 Comments
 
LVL 11

Assisted Solution

by:Zontar
Zontar earned 200 total points
ID: 9901512
Is SELECT S_NAME,SERVICE FROM SERVICE a valid query? Do you have columns named S_NAME and SERVICE in the SERVICE table?

You should respect case-sensitivity for table and column names in MySQL. MySQL on Windows is case-insensitive, but on other operating systems, it's not.

The convention in MySQL is to use lowercase for names of databases, columns, and tables, and uppercase for MySQL keywords. In addition there's a configuraiton option that forces these to be converted to lowercase, even if the table creation statement shows them in uppercase. (However, MySQL keywords are not case-sensitive on any operating system, this is just a convention used to make queries easier to read.)

Use the same case in both PHP and MySQL as well. Your query has S_NAME, but then in the PHP code, you try to write the value $rs["s_name"].

Perhaps you should try

SELECT s_name,service FROM service

or, since you're only writing the value of the S_NAME or s_name column (whichever it is)

SELECT s_name FROM service

Good luck.
0
 
LVL 24

Accepted Solution

by:
shivsa earned 300 total points
ID: 9902739
also change
$rs = mysql_query($sql,$dbcnx);
to
$rs = mysql_query($sql,$dbcnx) or die ("Could not execute query. $sql");

u will know if this part is ok and u are getting $rs ok then u can check whether u are passing correct argument to mysql_fetch_array or not.

0
 
LVL 24

Expert Comment

by:shivsa
ID: 9902750
or for better debugging.

$rs = mysql_query($sql,$dbcnx) or die ("Query '$sql' failed with error message: \"" . mysql_error () . '"');
0
 
LVL 19

Expert Comment

by:Michael701
ID: 9903499
i think your sql is the problem. you MUST qualify the field name when it is the same as the table name.

$sql = "SELECT S_NAME,SERVICE.SERVICE FROM SERVICE";
echo mysql_errno() . ": " . mysql_error(). "\n";

p.s. to resolve this i normally use plural tables

$sql = "SELECT S_NAME,SERVICE FROM SERVICES";

0
 
LVL 11

Expert Comment

by:Zontar
ID: 9903656
>  you MUST qualify the field name when it is the same as the table name.

This is incorrect.

While it's true that it is not a good practice to name a column with the same name as the table it's in, MySQL has no problem with it whatsiever if you choose to do so -- and no special syntax is required to access the column:

CREATE TABLE sometable (
  id INT(8) AUTO_INCREMENT PRIMARY KEY,
  sometable VARCHAR(25)
);

INSERT INTO sometable VALUES (('', 'foo'), ('', 'bar'), ('', 'baz'));

SELECT id,sometable FROM sometable;

id     sometable
---    -----
1      foo
2      bar
3      baz

3 rows in set (0.00 sec)
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Split wordpress loop 35 56
Need to remove %20 from url in get 17 36
Delete  php pages of a part of our site 8 31
AJAX Wordpress Not Reading Variable 2 27
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question