bnicholson.nc Posted January 22, 2009 Share Posted January 22, 2009 Hi, I am having a problem connecting to my database using mysql_connect(). What I am trying to do is use an include file to store the values for host, user, password, and database. This is what I have so far... (settings.inc) <?php $host="localhost"; $user="root"; $password="password"; $db="accountdb"; ?> (create.php) <?php include("./settings.inc"); // Connects to your Database mysql_connect($host, $user, $password); mysql_select_db($db); //This makes sure they did not leave any fields blank if (!$_POST['username'] | !$_POST['pass'] | !$_POST['pass2'] | !$_POST['email'] | !$_POST['expset']) { die('You did not complete all of the required fields'); } // checks if the username is in use if (!get_magic_quotes_gpc()) { $_POST['username'] = addslashes($_POST['username']); } $usercheck = $_POST['username']; $check = mysql_query("SELECT username FROM account WHERE username = '$usercheck'") or die(mysql_error()); $check2 = mysql_num_rows($check); //if the name exists it gives an error if ($check2 != 0) { die('Sorry, the username '.$_POST['username'].' is already in use.'); } // this makes sure both passwords entered match if ($_POST['pass'] != $_POST['pass2']) { die('Your passwords did not match. '); } // here we encrypt the password and add slashes if needed $pw=strtoupper($_POST['pass']); $un=strtoupper($_POST['username']); $pass = sha1($un.":".$pw); // now we insert it into the database $insert = "INSERT INTO account (username, sha_pass_hash, email) VALUES ('".$un."', '".$pass."', '".$_POST['email']."')"; $add_member = mysql_query($insert); ?> <h1>Registered</h1> <p>Thank you, you have registered - you may now login</a>.</p> (registration.php) <html> <head> <title>Registration Page</title> </head> <body bgcolor="#000000" alink="#FFFF00" vlink="#800080" link="#FF0000" text="#FFFFFF"> <form action="./create.php" method="post"> <table border="0"> <tr><td>Username:</td><td> <input type="text" name="username" maxlength="60"> </td></tr> <tr><td>Password:</td><td> <input type="password" name="pass" maxlength="10"> </td></tr> <tr><td>Confirm Password:</td><td> <input type="password" name="pass2" maxlength="10"> </td></tr> <tr><td>eMail:</td><td> <input type="text" name="email" maxlength="25"> </td></tr> <tr><th colspan=2><input type="submit" name="submit" value="Register"></th></tr></table> </form> </body> </html> _____________________________________________________________ This code is no where near complete, but I can not figure out what the problem is. When I enter the information into the fields and hit the Register button, I get a screen that tells me "No database selected" Any help will be greatly appreciated. Quote Link to comment Share on other sites More sharing options...
justinh Posted January 22, 2009 Share Posted January 22, 2009 change the settings.inc to this <?php $host="localhost"; $user="root"; $password="password"; $db="accountdb"; mysql_connect($host,$user,$password) or die(mysql_error()); mysql_select_db($db) or die(mysql_error()); ?> Quote Link to comment Share on other sites More sharing options...
gevans Posted January 22, 2009 Share Posted January 22, 2009 You should put some error checking in place so at least you can figure out where the problem is; mysql_conenct() The aboce link will take you to php.net and show you some good ways of connecting while error checking like this... <?php $link = mysql_connect('localhost', 'mysql_user', 'mysql_password'); if (!$link) { die('Could not connect: ' . mysql_error()); } echo 'Connected successfully'; Also anyone will be able to access settings.inc. why not just call it settings.inc.php Quote Link to comment Share on other sites More sharing options...
mazman13 Posted January 22, 2009 Share Posted January 22, 2009 I would also change your .inc file to a php. The php could be read if pulled up directly. Quote Link to comment Share on other sites More sharing options...
bnicholson.nc Posted January 22, 2009 Author Share Posted January 22, 2009 Thank you for your help and suggestions. I can now continue working on this project without racking my brain trying to figure it out! Quote Link to comment 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.