Q&A: Please help me link my mysql database to my website using php. I keep getting a wierd error msg!?
Posted August 10th, 2010
Question by Yiaggi: Please help me link my mysql database to my website using php. I keep getting a wierd error msg!?
Hi guys,
I have tried to post this question a couple of times but have made a few spelling mistakes in my coding & have only got answers regarding that! If you have the time to read through this then I will give you as many of those ‘star’ thingies as you want for the correct answer! This problem has become an ultimate pain in the ass and is taking days to solve so you can consider it your good deed for the day!
[NB I have changed my actual username & password for 'myusername' & 'mypassword' so that I can protect my details. I know it wont work without me entering them!]
Right …. here goes!
I have a website that I need to add a simple guestbook too. I am a web designer but this is my first go at programming etc.
*I have opened a database connection through my web servers website & acquired my username & set my password.
*I have my mySQL database set up and called ‘sm22628′
*Within that I have a table called ‘guestbook’ to hold the information entered on my page.
*In that table is “id (primary key), name, email, comment, datetime.
*On my website I have 3 php pages called:
guestbook.php
addguestbook.php
viewguestbook.php [all coding displayed below]
I have uploaded all of this to my server and when I try to post my first guestbook message I get a random message that say’s:
“Table ‘sm22628.guestbook’ doesn’t exist”
—————————————————————————————————-
This is very strange for 2 reason:
1/ It obviously does exist – I wrote it and can access it through the mySQL command prompt.
2/ The table that is suggested ‘doesn’t exist’ is named as ‘sm22628.guestbook’ which is obviously a mixture of the database name and the table name. As you can see below in my coding – it doesnt appear like that once!
—————————————————————————————————
I feel I am close to getting this working – is just this last niggly bit and I will be away! Can anyone tell me why this is happening?! Any help would be truly appreciated! Please find below – the php coding for my 3 pages.
Kind Regards
—————————————————————————————————-
ADDGUESTBOOK.PHP
———————————–
$host="localhost"; // Host name
$username="myusername"; // Mysql username
$password="mypassword"; // Mysql password
$db_name="sm22628"; // Database name
$tbl_name="guestbook"; // Table name
$db_connection = mysql_connect($host, $username, $password)or die("cannot connect server ");
mysql_select_db($db_name, $db_connection)or die("cannot select DB ". mysql_error());
$datetime=date("y-m-d h:i:s"); //date time
$sql="INSERT INTO guestbook(name, email, comment, datetime)VALUES('$name', '$email', '$comment', '$datetime')";
$result=mysql_query($sql) or die (mysql_error());
//check if query successful
if($result){
echo "Successful";
echo "
“;
echo “View guestbook“; // link to view guestbook page
}
else {
echo “ERROR – query not successfull”;
}
mysql_close();
?>
VIEWGUESTBOOK.PHP
————————————-
$host="localhost"; // Host name
$username="myusername"; // Mysql username
$password="mypassword"; // Mysql password
$db_name="sm22628"; // Database name
$tbl_name="guestbook"; // Table name
$db_connection = mysql_connect($host, $username, $password)or die("cannot connect server ");
mysql_select_db($db_name, $db_connection)or die("cannot select DB ". mysql_error());
$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);
while($rows=mysql_fetch_array($result)){
?>
GUESTBOOK.PHP
—————————-
| View Guestbook |






You should learn to debug “on-line”, that is, add displayed infos in case something goes wrong, or a not to say it went ok, so when you have an error, you can find out where it comes from your code!
Since it says “table does not exist”, that is where to look…
First, you don’t tell us in which “script” you have an error: could be in any of the three…
Let us have a look: ADDGUESTBOOK“);
$db_connection = mysql_connect($host, $username, $password)or die(“cannot connect server “);
mysql_select_db($db_name, $db_connection)or die(“cannot select DB “. mysql_error());
==> no need to use the parameter $db_connection (you are already there).
Does not seem to be here.
You can prove it with:
echo (“connection: ” . $dbconnection . “
=> this will print something like “ressource #2″.
$sql=”INSERT INTO guestbook(name, email, comment, datetime)VALUES(‘$name’, ‘$email’, ‘$comment’, ‘$datetime’)”;
$result=mysql_query($sql) or die (mysql_error());
==> Here we have a few problems:
1 ==> The syntax for is is “imperfect”
$sql = “INSERT INTO `guestbook`(`name`, `email`, `comment`, `datetime` ) VALUES ( ‘ ” . $name . ” ‘, ‘ ” . $email . ” ‘ , ‘ ” . $comment . ” ‘ , ‘ ” . $datetime . ” ‘ ) ” ;
(spaces added: beware difference between ` ‘ “.)
2 ==> You CANNOT have a field in your table called “datetime”: it is an SQL keyword!!! Use “datum”, “date_time” or similar.
==> The problem could reside here, as guestbook is not quoted.
My guess is that you do not pass this point…
To check, change
$result=mysql_query($sql) or die (mysql_error());
into:
$res = mysql_query ($sql) or die (“Fail trying to insert into table guestbook: ” . mysql_error() );
==> This is more explicit than mysql_error() which is usually rather vague!
A final note: when you are done, please kill the link!
=> mysql_close($db_connection);