Not able to connect to MySQL DB using PHP

Status
Not open for further replies.

roblav96

New Member
Messages
2
Reaction score
0
Points
0
I'm trying to connect to my MySQL database using PHP code and I'm getting this error:
Code:
Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'roblav96_baked96'@'74.63.233.5' (using password: YES) in /home/roblav96/public_html/sb/shoutbox.php on line 46

Warning: mysql_select_db() [function.mysql-select-db]: Access denied for user 'roblav96'@'74.63.233.5' (using password: NO) in /home/roblav96/public_html/sb/shoutbox.php on line 47

Warning: mysql_select_db() [function.mysql-select-db]: A link to the server could not be established in /home/roblav96/public_html/sb/shoutbox.php on line 47
Access denied for user 'roblav96'@'74.63.233.5' (using password: NO)

This is the code I'm using:
PHP:
<style type="text/css">
<!--
#contentbox {
background: #E5E5E5;
padding: 5px;
width: 200px;
height: 200px;
overflow: auto; }
ul#shoutboxmessage {
margin: 0;
padding: 0;
list-style-type: none;
color: #000000;
font: normal 10px verdana,tahoma,arial; }
-->
</style>
<?php
#require_once("config.php");
$dbhost = "mysql-cossacks.x10hosting.com";
$dbname = "roblav96_shoutbox";
$dbuser = "roblav96_baked96";
$dbpass = "mypassword";

$name = $_POST['name'];
$message = $_POST['message'];
$ip = $_POST['ip'];
$mlen = strlen($message);
$maxlength = 150;
$date = date("M jS Y");
if ($_POST['submit']) {
if ($name == "") {
echo "<strong>Error: Please enter your nickname.</strong>";
}
else if ($message == "") {
echo "<strong>Error: No message to be sent.</strong>";
}
else if ($mlen > $maxlength) {
echo "<strong>Error: Message too long.</strong>";
}
else {
$db = mysql_connect($dbhost,$dbuser,$dbpass);
mysql_select_db($dbname) or die(mysql_error());
mysql_query("INSERT INTO shoutbox(name,message,date,ip) VALUES('$name','$message','$date','$ip')");
}
}
$db = mysql_connect($dbhost,$dbuser,$dbpass);
mysql_select_db($dbname) or die(mysql_error());
$query = "SELECT * FROM shoutbox ORDER BY id DESC LIMIT 20";
$result = mysql_query($query);
echo "<div id=\"contentbox\">\n";
echo "<ul id=\"shoutboxmessage\">\n";
while($r = mysql_fetch_array($result)) {
$name = $r['name'];
$name = strip_tags($name);
$message = $r['message'];
$message = strip_tags($message);
echo "<li><strong>>$name</strong>: $message</li>\n";
}
echo "</ul>\n";
echo "</div>\n";
mysql_close($db);
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<strong>Nickname:</strong><br/>
<input type="text" name="name" maxlength="20"><<br/>
<strong>Message:</strong><br/>
<textarea name="message"></textarea><br/>
<input type="submit" name="submit" value="Shout It!">
<input type="hidden" name="ip" value="<?php echo $_SERVER['REMOTE_ADDR']; ?>">
</form>
</div>
?>
 

diabolo

Community Advocate
Community Support
Messages
1,682
Reaction score
32
Points
48
just as a precaution.
does your shoutbox auto-refresh?
if it does, then it violates x10's TOS due to the significant amount of mySQL resources it takes.
 

descalzo

Grim Squeaker
Community Support
Messages
9,373
Reaction score
326
Points
83
Instead of

$dbhost = "mysql-cossacks.x10hosting.com";

try

$dbhost = "localhost";

Edit/Add:

You might want to use htmlentities instead of strip_tags:

$name = $r['name'];
$name = htmlentities($name);
$message = $r['message'];
$message = htmlentities($message);

Edit2:

Actually,it is better to sanitize the name and message before you save them. That way you only have to do it once.
 
Last edited:

tuxette

New Member
Messages
10
Reaction score
0
Points
0
Gosh, I am also not able to connect to MySQL with PHP. However, this may be important:

  • This is the first time I use PHP. I would love to use JSP, as I have done many things with it, but there is no free and good hosting for such coding... that I know of.
  • I just created my account and site.

What I am attempting to do is select this one record in order to login into the page. I will delete the values of some of the variables for privacy, but this is it:
Code:
$host="localhost";
$username=// Mysql username
$password=// Mysql password
$db_name=// Database name
$tbl_name=// Table name
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
(...)
$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypass'";
$result=mysql_query($sql);
The error pops up at the mysql_connect line.



PS: Sorry for intruding into the topic, but it may be that there is something going on between PHP and MySQL on this host service right now.
 
Status
Not open for further replies.
Top