Isaac
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 /displaySe rvices.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.
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/anudaysp/public_html
Below is my code:
#CONNECT TO MYSQL
$dbcnx = @mysql_connect('localhost'
#CONNECT TO DATABASE
@mysql_select_db($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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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";
$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";
> 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)
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)
$rs = mysql_query($sql,$dbcnx) or die ("Query '$sql' failed with error message: \"" . mysql_error () . '"');