Jump to content

I cannot resolve connection to database fail


akrohn

Recommended Posts

Thanks for reading this. I have just begun using php, and I cannot, for my life, figure out what I am doing wrong here. I want to submit a form for a person to register with my site. It's simple, just name, email and password. I keep getting an error that the connection failed, so I will just show you what I have up until that point.

 

My form is Post method, and it looks like:

 

<form method="post" action="signup.php" enctype="multipart/form-data">
<input name="name" type="text" id="name">
<input name="email" type="text" id="email">
<input name="pass" type="password">
<input type="submit" name="Submit" value="Submit">
</form>

 

And the connection script is like so:

 

<?php
$username="db_username";
$password="db_password";
$database="db_name";

$dbc=mysql_connect ("localhost", "$username", "$password") 
       or die ('I cannot connect to the database.');
mysql_select_db ("$database", "$dbc");

 

It's the 'I cannot connect to the database.' error that I keep getting. In case it helps, the rest of the thing looks like this:

 

<?php
$name= $_POST['name'];
$email= $_POST['email'];
$pass= $_POST['pass'];

$query = "INSERT INTO table (name, email, password) VALUES ('$name', '$email', '$pass')";
mysql_query($query);

mysql_close();

I have deliberately left out the php opening and closing tags (<?php...?>). Thanks so much for any help.

 

Adam

 

MOD EDIT:

 . . . 

BBCode tags added.

When posting code, enclose it within the forum's,

 . . . 

BBCode tags.

 

Also, post the error message you're getting. Those usually contain specific information that can help diagnose problems.

sorry about the code tags missing.

 

I am not getting any error except for the one that I have created 'I cannot connect to the database'.

 

When I get that error, there is just an empty page following. However, I removed that error now to see what would happen, and I don't get any errors. The page loads, and the only way I know that nothing happened is because I log into phpMyAdmin and checked there, and I can see that nothing was inserted.

One presumes this...

<?php
$username="db_username"; /* this is not your real username and you ARE subbing the real name when you try it */
$password="db_password"; /* same comment here */
$database="db_name"; /* same comment here */

$dbc=mysql_connect ("localhost", $username, $password) 
       or die ('I cannot connect to the database.' . , mysql_error ( )];
mysql_select_db ($database, $dbc);

To the last post, yes that is correct. They are not the real username, etc.

 

For the error message, I put in a mysql_error() tag and it gave me this:

 

Access denied for user 'db_username'@'localhost' (using password: YES)

 

Trying to find out now from my host why i am getting this error.

Maybe this. I am not really familiar with the collation setting. I see that in my table it is latin1_swedish_ci, but I see that in the general settings it is utf8_general_ci.

 

I seem to remember reading that they need to be the same or something. Could that be the problem?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.