views:

34

answers:

2

Hello,

I am very new to php and trying to work stuff around as they come across.

I am trying to display the output of a query on the web page. Following is the code.

<?php
$username="xxxxxxxxx";
$password="xxxxxxxxx";
$database="xxxxxxxxx";

mysql_connect('localhost',$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM user";

$result = mysql_query ($query) or die(mysql_error());
$num=mysql_numrows($result) or die(mysql_error());

mysql_close();

echo "<b><center>Database Output</center></b><br><br>";

$i=0;
while ($i < $num) {

$zip=mysql_result($result,$i,"zipcode");
$area=mysql_result($result,$i,"area");
$city=mysql_result($result,$i,"city");

echo "<b>Zip: $zip</b><br>Area: $area<br>City: $city<br><hr><br>";

$i++;
}
?>

The code runs file on the terminal window. However, when I try to execute the file from a browser ,I get the following error message:

Notice: Use of undefined constant localhost - assumed 'localhost' in /users/home/xxxx/web/public/query.php on line 6 SELECT command denied to user 'xxxxxx'@'localhost' for table 'city'

Why does it say select command denied ??

A: 

I think there's a syntax error here. Fix this first and try again:

"SELECT * FROM user'";

to

"SELECT * FROM user";

Also, it sounds like the database user doesn't have the privelege to read the table. Check the permissions in phpmyadmin or whatever mysql admin tool you use.

The Pixel Developer
Thanks for the response. That was a typo while writing the question. The db user has permissions because, I am able to execute it on my shell terminal like "php sql.php" . This gives the expected result from the table. I am wondering why I cannot print to the browser and why this wired error.
p1
Yes it was copy pasted. Infact the quotes was something that caused the problem. However after putting them in , I still got the error and so I posted it here. Turns out that it was solved after I flushed the cookies and browser history !!@lotoffreetime: Thnx for the reply. I could have accepted your response as solution if it was not under a comment.
p1
A: 

Yes it was copy pasted. Infact the quotes was something that caused the problem. However after putting them in , I still got the error and so I posted it here. Turns out that it was solved after I flushed the cookies and browser history !! @lotoffreetime: Thnx for the reply. I could have accepted your response as solution if it was not under a comment.

p1