Page 1 of 1

PHP SQL Error Newbie can't understand!

Posted: Fri Jan 02, 2009 10:08 am
by Flashart80
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

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()
} 
 
So my entire code looks like;

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

Re: PHP SQL Error Newbie can't understand!

Posted: Fri Jan 02, 2009 10:15 am
by thisismyurl
Peter, there are a couple of potential reasons for your problem here but my first reaction would be that if you're in a shared hosting environment (most people are), you are most likely lacking the account details in your database name.

For example, my website http://www.thisismyurl.com uses a database named timuwp but to access it, I need to call thisismy_timuwp which represents the account (usually the FTP login) plus an underscore, plus the database name.

Give that a try and see if it helps.

Re: PHP SQL Error Newbie can't understand!

Posted: Fri Jan 02, 2009 11:03 am
by Flashart80
Hello,

You are right that I am in a shared environment. Thank you for the FTP hint. I have incorporated that and I have now got the result I have been after!

Books are great but never as good as someone who knows!

Many thanks, I am ever so grateful!

Kind regards

Peter