GameYin Posted February 11, 2008 Share Posted February 11, 2008 Ok so I have a register/login script. I have a problem. Won't let me connect to the database. Here is my register.php page <? session_start(); include("database.php"); function usernameTaken($username){ global $conn; if(!get_magic_quotes_gpc()){ $username = addslashes($username); } $q = "select username from users where username = '$username'"; $result = mysql_query($q,$conn); return (mysql_numrows($result) > 0); } function addNewUser($username, $password){ global $conn; $q = "INSERT INTO users VALUES ('$username', '$password')"; return mysql_query($q,$conn); } function displayStatus(){ $uname = $_SESSION['reguname']; if($_SESSION['regresult']){ ?> <h1>Registered!</h1> <p>Thank you <b><? echo $uname; ?></b>, your information has been added to the database, you may now <a href="main.php" title="Login">log in</a>.</p> <? } else{ ?> <h1>Registration Failed</h1> <p>We're sorry, but an error has occurred and your registration for the username <b><? echo $uname; ?></b>, could not be completed.<br> Please try again at a later time.</p> <? } unset($_SESSION['reguname']); unset($_SESSION['registered']); unset($_SESSION['regresult']); } if(isset($_SESSION['registered'])){ ?> <? return; } if(isset($_POST['subjoin'])){ /* Make sure all fields were entered */ if(!$_POST['user'] || !$_POST['pass']){ die('You didn\'t fill in a required field.'); } /* Validation... */ $_POST['user'] = trim($_POST['user']); if(strlen($_POST['user']) > 30){ die("Sorry, the username is longer than 30 characters, please shorten it."); } /* Check if username is already in use */ if(usernameTaken($_POST['user'])){ $use = $_POST['user']; die("Sorry, the username: <strong>$use</strong> is already taken, please pick another one."); } /* Add the new account to the database */ $md5pass = md5($_POST['pass']); $_SESSION['reguname'] = $_POST['user']; $_SESSION['regresult'] = addNewUser($_POST['user'], $md5pass); $_SESSION['registered'] = true; echo "<meta http-equiv=\"Refresh\" content=\"0;url=$HTTP_SERVER_VARS[php_SELF]\">"; return; } else{ ?> <html> <title>Registration Page</title> <body> <h1>Register</h1> <form action="<? echo $HTTP_SERVER_VARS['PHP_SELF']; ?>" method="post"> <table align="left" border="0" cellspacing="0" cellpadding="3"> <tr><td>Username:</td><td><input type="text" name="user" maxlength="30"></td></tr> <tr><td>Password:</td><td><input type="password" name="pass" maxlength="30"></td></tr> <tr><td colspan="2" align="right"><input type="submit" name="subjoin" value="Join!"></td></tr> </table> </form> </body> </html> <? } ?> Here is my database.php. Also I need help to know what I put in the ****** <? $conn = mysql_connect("localhost", "*****", "******") or die(mysql_error()); mysql_select_db('******', $conn) or die(mysql_error()); ?> Quote Link to comment https://forums.phpfreaks.com/topic/90540-register-login-script/ Share on other sites More sharing options...
peranha Posted February 11, 2008 Share Posted February 11, 2008 $conn = mysql_connect("localhost", "sqlusername", "sqlpassword") or die(mysql_error()); mysql_select_db('sqldatabasename', $conn) or die(mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/90540-register-login-script/#findComment-464180 Share on other sites More sharing options...
GameYin Posted February 11, 2008 Author Share Posted February 11, 2008 Ok so I have a website gameyin.com, I normally connect with gameyinc. I will post my password here since noone will know which host I use therefore no risk I presume Would I do something like this: $conn = mysql_connect("localhost", "gameyinc", "3579514862") or die(mysql_error()); mysql_select_db('users', $conn) or die(mysql_error()); The 3579514862 isn't my actual password but that is just an example. My database is named "users" so it will connect to gameyinc_users? Quote Link to comment https://forums.phpfreaks.com/topic/90540-register-login-script/#findComment-464194 Share on other sites More sharing options...
peranha Posted February 11, 2008 Share Posted February 11, 2008 Is the database name, or the table users. If the table is users, you will have to put in the DB name Quote Link to comment https://forums.phpfreaks.com/topic/90540-register-login-script/#findComment-464197 Share on other sites More sharing options...
GameYin Posted February 11, 2008 Author Share Posted February 11, 2008 Just for easy sakes, I named the table users and the database users. Quote Link to comment https://forums.phpfreaks.com/topic/90540-register-login-script/#findComment-464209 Share on other sites More sharing options...
GameYin Posted February 12, 2008 Author Share Posted February 12, 2008 Help still needed... Quote Link to comment https://forums.phpfreaks.com/topic/90540-register-login-script/#findComment-464907 Share on other sites More sharing options...
revraz Posted February 12, 2008 Share Posted February 12, 2008 With? Quote Link to comment https://forums.phpfreaks.com/topic/90540-register-login-script/#findComment-464917 Share on other sites More sharing options...
GameYin Posted February 12, 2008 Author Share Posted February 12, 2008 Getting it connected... I get an error. It says failed to connect to database 'gameyinc_users'@'localhost' What's up with this. Quote Link to comment https://forums.phpfreaks.com/topic/90540-register-login-script/#findComment-464993 Share on other sites More sharing options...
revraz Posted February 12, 2008 Share Posted February 12, 2008 Is your DB named that? I thought you said it was just "users" Quote Link to comment https://forums.phpfreaks.com/topic/90540-register-login-script/#findComment-465011 Share on other sites More sharing options...
GameYin Posted February 12, 2008 Author Share Posted February 12, 2008 Well it is, but I can't just connect to "users@localhost". Doesn't it connect to my root+mydbname? So gameyinc_users would be correct. So any help why this is happening to me? Quote Link to comment https://forums.phpfreaks.com/topic/90540-register-login-script/#findComment-465145 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.