[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Query Selection Help

Posted on 2003-12-08
5
Medium Priority
?
264 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses

649 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