Forum Moderators: coopster
I'm having a problem connecting to a database, when i use the following script to connect to a database (all the login details are correct) it connects to mysql but says it can't connect to the database any body got any suggestions,
<?php
$db_username = "name";
$db_password = "password";
$db_hostname = "localhost";
$db_connect = mysql_connect($db_hostname, $db_username, $db_password)or die("Unable to connect to MySQL");
$db_select = mysql_select_db("360tv_org_uk_-_news", $db_connect)or die("Unable to select news");
// additional functionality here
echo "You have successfully connected to MySQL!";
mysql_close($db_connect);
// grab the variables from the form
$author = $_POST['author'];
$title = $_POST['title'];
$news = $_POST['news'];
// insert the data into the database
$query = "INSERT INTO news (author, title, news)
VALUES('$author', '$title', '$news')";
mysql_query($query) or die(mysql_error());
echo "Thanks for submitting!";
?>
The variables come from a form which is this
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<form name="newsubmit" method="post" action="nsubmit.php">
By: <input type="text" name="author"><br>
Title: <input type="text" name="title"><br>
News: <textarea name="news" rows="6" cols="24"></textarea><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
Any help would be great
if u can login to mysql and cant select database then generally there can be 2 reasons
1) database doesnt exist
2) current user doesnt have rights to access that db
your case is prolly the second one, give that a try ;)
$db_connect = @mysql_connect($db_hostname, $db_username, $db_password) or die("Unable to connect to MySQL");
$db_select = @mysql_select_db("360tv_org_uk_-_news", $db_connect) or die("Unable to select news");
Don't know for sure if this will make any difference.
Chris
$db_connect = mysql_connect($db_hostname, $db_username, $db_password);
if($db_connect)
{
$db_select = mysql_select_db("360tv_org_uk_-_news", $db_connect);
if (!$db_select)
{
//Email alert DB select failed
die("Unable to select news");
}
}
else
{
//Email alert connection failed
die ("Unable to connect to MySQL");
}
And you could also fire off Emails or whatever to alert yourself to the problem.