Solved

Query Selection Help

Posted on 2003-12-08
5
218 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
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.

705 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now