-2

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

codeObserver
  • 6,521
  • 16
  • 76
  • 121
  • See also [my attempt at a canonical answer for causes of this error message](http://stackoverflow.com/questions/2941169/what-does-the-php-error-message-notice-use-of-undefined-constant-mean/8025500#8025500). – John Carter Nov 06 '11 at 06:52
  • [**Please, don't use `mysql_*` functions in new code**](http://bit.ly/phpmsql). They are no longer maintained [and are officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). See the [**red box**](http://j.mp/Te9zIL)? Learn about [*prepared statements*](http://j.mp/T9hLWi) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://j.mp/QEx8IB) will help you decide which. If you choose PDO, [here is a good tutorial](http://j.mp/PoWehJ). – Madara's Ghost Jun 01 '13 at 09:05

2 Answers2

0

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
  • 13,282
  • 10
  • 43
  • 60
  • 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. – codeObserver Aug 01 '10 at 01:08
  • Is this code sample cut & pasted from the source? The error looks like how it would appear if you had missed the quotes from around 'localhost', i.e. PHP encountering a symbol called localhost rather than a variable ($localhost) or literal string ("localhost" or 'localhost'). – lotsoffreetime Aug 01 '10 at 10:59
  • 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. – codeObserver Aug 01 '10 at 23:26
0

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.

codeObserver
  • 6,521
  • 16
  • 76
  • 121