PHP SQL Error Newbie can't understand!
Posted: Fri Jan 02, 2009 10:08 am
hello everyone
I have had a search in the forums for the resolution but I am unable to resolve what is probably very simple.
I am by the way, completely new to PHP so please be gentle! I have bought the book "Build your own database driven website" by Sitepoint in which you build a joke database called "ijdb". I have this database on a server. I am now trying to code a php page as per the instructions in the book which tell me to create a connection in php using the syntax.
This is fine and I can connect to my MySql Server no problems. I have an if statement to show an error if it goes wrong and this doesn't appear, my html code (which just says "I am connected") does show, so I presume everything is ok.
The problem arises when i try to connect to a specific database of which "ijdb" is one of three. The book says to use the following code and if statement
So my entire code looks like;
When I execute this I get "Unable to locate the joke database at this timeAccess denied for user 'username'@'%' to database 'ijdb.
This suggests to me firstly that the access parameters are incorrect. However if I take out all of the code except the first 2 lines of php, the html shows on the page.
Can someone point me in the right direction? Is this something at the server end I need to change? I am going around in circles!!
Thanks ever so much for any help.
Kind regards
Peter
I have had a search in the forums for the resolution but I am unable to resolve what is probably very simple.
I am by the way, completely new to PHP so please be gentle! I have bought the book "Build your own database driven website" by Sitepoint in which you build a joke database called "ijdb". I have this database on a server. I am now trying to code a php page as per the instructions in the book which tell me to create a connection in php using the syntax
Code: Select all
$dbcnx = mysql_connect('localhost', 'root', 'mypasswd');This is fine and I can connect to my MySql Server no problems. I have an if statement to show an error if it goes wrong and this doesn't appear, my html code (which just says "I am connected") does show, so I presume everything is ok.
The problem arises when i try to connect to a specific database of which "ijdb" is one of three. The book says to use the following code and if statement
Code: Select all
mysql_select_db('ijdb', $dbcnx);
if (!@mysql_select_db('ijdb')) {
exit('<p>Unable to locate the joke ' .
'database at this time.</p>');mysql_error()
}
Code: Select all
<?php
error_reporting(E_ALL);
$dbcnx = mysql_connect('ipaddress', 'username', 'password');
mysql_select_db('dbase', $dbcnx);
if (!@mysql_select_db('ijdb')) {
exit('<p>Unable to locate the joke ' .
'database at this time'. mysql_error() . '</p>');
}
?>
<p>Here are all the jokes in our database:</p>
<blockquote>
<?php
// Request the text of all the jokes
$result = @mysql_query('SELECT joketext FROM joke');
if (!$result) {
exit('<p>Error performing query: ' . mysql_error() . '</p>');
}
// Display the text of each joke in a paragraph
while ($row = mysql_fetch_array($result)) {
echo '<p>' . $row['joketext'] . '</p>';
}
?>
<p>I am connected</p>
</blockquote>
</body>
</html>When I execute this I get "Unable to locate the joke database at this timeAccess denied for user 'username'@'%' to database 'ijdb.
This suggests to me firstly that the access parameters are incorrect. However if I take out all of the code except the first 2 lines of php, the html shows on the page.
Can someone point me in the right direction? Is this something at the server end I need to change? I am going around in circles!!
Thanks ever so much for any help.
Kind regards
Peter