Please help with a problem connecting to a mysql database using php. Keep

  • Thread starter Thread starter Yiaggi
  • Start date Start date
Y

Yiaggi

Guest
getting "Cant connect to DB" error.? Hi guys,

I have a small problem connecting my simple mysql guest book to my website using php.

Basically I have a mysql database called "mrcagb"

Within that database I have a table called "guestbook".

I have my 3 php files in my website:

guestbook.php
addguestbook.php
viewguestbook.php

I have had the coding checked over and apparently that is fine but I am still getting the same error message whenever I try to post my first item on the guestbook:
--------------------------------------...
cannot select DB Access denied for user '*myusername*' to database 'mrcagb'.
--------------------------------------...
(I have changed my actual user name to *myusername* for the purposes of this post).

The username and password I have been using are the ones provided by my hosting provider. I had to go onto their website and set up a mysql database function where upon it gave me the details to use.

Am I not using the right details?! I am well confused and had been getting my head around the whole mysql / php subject before I hit this wall! If anyone is kind enough to help I will be forever grateful!

Kind regards & ta in advance! :)

PS .... for those that can be bothered to read on .... my php code for the 3 pages is below if that helps?!

GUESTBOOK.PHP
----------------------------
<table width="400" border="0" align="center" cellpadding="3" cellspacing="0">
<tr>
<td><strong>Test Sign Guestbook </strong></td>
</tr>
</table>
<table width="400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form id="form1" name="form1" method="post" action="addguestbook.php">
<td>
<table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td width="117">Name</td>
<td width="14">:</td>
<td width="357"><input name="name" type="text" id="name" size="40" /></td>
</tr>
<tr>
<td>Email</td>
<td>:</td>
<td><input name="email" type="text" id="email" size="40" /></td>
</tr>
<tr>
<td valign="top">Comment</td>
<td valign="top">:</td>
<td><textarea name="comment" cols="40" rows="3" id="comment"></textarea></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td><input type="submit" name="Submit" value="Submit" /> <input type="reset" name="Submit2" value="Reset" /></td>
</tr>
</table>
</td>
</form>
</tr>
</table>
<table width="400" border="0" align="center" cellpadding="3" cellspacing="0">
<tr>
<td><strong><a href="viewguestbook.php">View Guestbook</a> </strong></td>
</tr>
</table>

[THAT SEEMS TO WORK FINE & OFFER A PAGE TO ENTER THE MESSAGE ETC]

ADD GUESTBOOK.PHP
------------------------------------

<?php
$host="localhost"; // Host name
$username="myusername"; // Mysql username
$password="mypassword"; // Mysql password
$db_name="mrcadb"; // Database name
$tbl_name="guestbook"; // Table name

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect server ");
mysql_select_db("$db_name")or die("cannot select DB");

$datetime=date("y-m-d h:i:s"); //date time

$sql="INSERT INTO $tbl_name(name, email, comment, datetime)VALUES('$name', '$email', '$comment', '$datetime')";
$result=mysql_query($sql);

//check if query successful
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='viewguestbook.php'>View guestbook</a>"; // link to view guestbook page
}

else {
echo "ERROR";
}

mysql_close();
?>

and then finally:

VIEWGUESTBOOK.PHP
-------------------------------------

<table width="400" border="0" align="center" cellpadding="3" cellspacing="0">
<tr>
<td><strong>View Guestbook | <a href="guestbook.php">Sign Guestbook</a> </strong></td>
</tr>
</table>
<br>

<?php

$host="localhost"; // Host name
$username="myusername"; // Mysql username
$password="mypassword"; // Mysql password
$db_name="mrcadb"; // Database name
$tbl_name="guestbook"; // Table name

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect server ");
mysql_select_db("$db_name")or die("cannot select DB");

$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);

while($rows=mysql_fetch_array($result)...
?>
<table width="400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td><table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td>ID</td>
<td>:</td>
<td><? echo $rows['id']; ?></td>
</tr>
<tr>
<td width="117">Name</td>
<td width="14">:</td>
<td width="357"><? echo $rows['name']; ?></td>
</tr>
<tr>
<td>Email</td>
<td>:</td>
<td><? echo $rows['email']; ?></td>
</tr>
<tr>
<td valign="top">Comment</td>
<td valign="top">:</td>
<td><? echo $rows['comment']; ?></td>
</tr>
<tr>
<td valign="top">Date/Time </td>
<td valign="top">:</td>
<td><? echo $rows['datetime']; ?></td>
</tr
 
Back
Top