?
Solved

Query Selection Help

Posted on 2003-12-08
5
Medium Priority
?
262 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 11

Assisted Solution

by:Zontar
Zontar earned 800 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 1200 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

Video: Liquid Web Managed WordPress Comparisons

If you run run a WordPress, you understand the potential headaches you may face when updating your plugins and themes. Do you choose to update on the fly and risk taking down your site; or do you set up a staging, keep it in sync with your live site and use that to test updates?

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

765 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