Link to home
Start Free TrialLog in
Avatar of Isaac
IsaacFlag for United States of America

asked on

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.
SOLUTION
Avatar of Zontar
Zontar

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
or for better debugging.

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

Avatar of Zontar
Zontar

>  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)