• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 269
  • Last Modified:

Query Selection Help

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
Isaac
Asked:
Isaac
  • 2
  • 2
2 Solutions
 
ZontarCommented:
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
 
shivsaCommented:
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
 
shivsaCommented:
or for better debugging.

$rs = mysql_query($sql,$dbcnx) or die ("Query '$sql' failed with error message: \"" . mysql_error () . '"');
0
 
Michael701Commented:
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
 
ZontarCommented:
>  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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now