akrohn Posted February 22, 2012 Share Posted February 22, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/257509-i-cannot-resolve-connection-to-database-fail/ Share on other sites More sharing options...
litebearer Posted February 22, 2012 Share Posted February 22, 2012 try changing this... $dbh=mysql_connect ("localhost", "$username", "$password") to this... $dbh=mysql_connect ("localhost", $username, $password) Quote Link to comment https://forums.phpfreaks.com/topic/257509-i-cannot-resolve-connection-to-database-fail/#findComment-1319832 Share on other sites More sharing options...
akrohn Posted February 22, 2012 Author Share Posted February 22, 2012 No dice. Thanks, though. Quote Link to comment https://forums.phpfreaks.com/topic/257509-i-cannot-resolve-connection-to-database-fail/#findComment-1319833 Share on other sites More sharing options...
Pikachu2000 Posted February 22, 2012 Share Posted February 22, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/257509-i-cannot-resolve-connection-to-database-fail/#findComment-1319834 Share on other sites More sharing options...
akrohn Posted February 22, 2012 Author Share Posted February 22, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/257509-i-cannot-resolve-connection-to-database-fail/#findComment-1319835 Share on other sites More sharing options...
litebearer Posted February 22, 2012 Share Posted February 22, 2012 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); Quote Link to comment https://forums.phpfreaks.com/topic/257509-i-cannot-resolve-connection-to-database-fail/#findComment-1319839 Share on other sites More sharing options...
akrohn Posted February 22, 2012 Author Share Posted February 22, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/257509-i-cannot-resolve-connection-to-database-fail/#findComment-1319840 Share on other sites More sharing options...
Pikachu2000 Posted February 22, 2012 Share Posted February 22, 2012 Temporarily change that to show any errors returned. or die( mysql_error() ); Quote Link to comment https://forums.phpfreaks.com/topic/257509-i-cannot-resolve-connection-to-database-fail/#findComment-1319841 Share on other sites More sharing options...
akrohn Posted February 22, 2012 Author Share Posted February 22, 2012 ok. my host can't help me. Could it be on their end? Got any good recommendations for another host? Quote Link to comment https://forums.phpfreaks.com/topic/257509-i-cannot-resolve-connection-to-database-fail/#findComment-1319842 Share on other sites More sharing options...
Pikachu2000 Posted February 22, 2012 Share Posted February 22, 2012 A problem like that is unlikely to be be the host's fault. Either your username, or your password, or both are wrong. Quote Link to comment https://forums.phpfreaks.com/topic/257509-i-cannot-resolve-connection-to-database-fail/#findComment-1319847 Share on other sites More sharing options...
akrohn Posted February 22, 2012 Author Share Posted February 22, 2012 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? Quote Link to comment https://forums.phpfreaks.com/topic/257509-i-cannot-resolve-connection-to-database-fail/#findComment-1319852 Share on other sites More sharing options...
Pikachu2000 Posted February 22, 2012 Share Posted February 22, 2012 And how are you able to get access to the database to check those values? Quote Link to comment https://forums.phpfreaks.com/topic/257509-i-cannot-resolve-connection-to-database-fail/#findComment-1319854 Share on other sites More sharing options...
akrohn Posted February 22, 2012 Author Share Posted February 22, 2012 phpMyAdmin. I just changed the database and tables to utf8_general_ci. Still no luck on the connection. Is there a place in my script that I can define that, or should define that? Quote Link to comment https://forums.phpfreaks.com/topic/257509-i-cannot-resolve-connection-to-database-fail/#findComment-1319855 Share on other sites More sharing options...
Pikachu2000 Posted February 22, 2012 Share Posted February 22, 2012 That isn't the problem. I'm about 99% sure you'll find that either your username or password is mistyped, or you've specified the wrong hostname. Quote Link to comment https://forums.phpfreaks.com/topic/257509-i-cannot-resolve-connection-to-database-fail/#findComment-1319858 Share on other sites More sharing options...
akrohn Posted February 22, 2012 Author Share Posted February 22, 2012 Oh, you bastards. It was my password. Only, I had it wrong when I set up the user for the database, rather than the one that I was using in my script. Thanks so much for your help, and sorry for taking up so much of your time. Quote Link to comment https://forums.phpfreaks.com/topic/257509-i-cannot-resolve-connection-to-database-fail/#findComment-1319863 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.