Forum Moderators: coopster

Message Too Old, No Replies

cant connect to mysql database

         

jack_keenan

3:13 pm on Jan 31, 2006 (gmt 0)

10+ Year Member



Hi there,

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

Anyango

3:49 pm on Jan 31, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The Mysql Username with which you are trying to login, does that user have access to that database which you are trying to access?

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 ;)

chriswragg

8:33 pm on Jan 31, 2006 (gmt 0)

10+ Year Member



You need to put the @ symbol before the two mysql functions at the start, as you are using 'OR'. Try it like this:

$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

FalseDawn

9:55 pm on Jan 31, 2006 (gmt 0)

10+ Year Member



I've never liked using die in an OR like that - IMO it would be better to do something like:

$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.