ChaosXero Posted August 11, 2006 Share Posted August 11, 2006 Everything gets passed to 'login' correctly, however it never manages to make it to the query inside the login function. (see comment) There may be other things wrong with the code, but I really just need to figure out why I cant get connected. P.S. All of the Database info works correctly on other pages, so it's not that (and yes, some things have been removed)[code]<?PHP define("host", ""); define("UNAME", ""); define("pword", ""); $conn = mysql_connect(host,UNAME,pword) or die(mysql_error()); $db = mysql_select_db('db173735005');function login($u, $p) { $sql = "SELECT * FROM users WHERE username = '".$u."'"; $result = mysql_query($sql, $conn) or die("Error!"); $res = mysql_fetch_assoc($result); echo "Test!\n\n"; echo $res['password_hash']; if ($res['password_hash'] === $p) { session_start(); $_SESSION['username'] = $u; $sesid = microtime().$u.microtime().rand(0, 1000); $_SESSION['sessid'] = md5($sesid); $_SESSION['accesslevel'] = $res['accesslevel']; $sql2 = "UPDATE `users` SET `sess_hash` = '".$_SESSION['sessid']."' WHERE `username` ='".$u."' LIMIT 1" ; $updated = mysql_query($sql2) or die("Not Updated!"); return 1; }else{ return 0; } }//End of Func_Login?><? if (isset($_POST['username']) && isset($_POST['pass'])) { if (!empty($_POST['username']) && !empty($_POST['pass'])) { $user = $_POST['username']; $pass = md5($_POST['pass']); mysql_real_escape_string($user); mysql_real_escape_string($pass); if (login($user, $pass) == 1) { echo "Logged In!"; echo "Add js here to push to next page."; } else { echo "Bad Username/Pass. Try again."; echo "$pass"; echo "$user"; } } } ?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/17263-mysql-connection-problem/ Share on other sites More sharing options...
corbin Posted August 11, 2006 Share Posted August 11, 2006 Uhhhhhh I just glanced through the rest of the script so there might be another problem... But i think $db = mysql_select_db(db173735005); should be $db = mysql_select_db('db173735005'); Quote Link to comment https://forums.phpfreaks.com/topic/17263-mysql-connection-problem/#findComment-73216 Share on other sites More sharing options...
ChaosXero Posted August 11, 2006 Author Share Posted August 11, 2006 Hmm... it works like that on the other pages. I'll try it though just in case.No Luck. Quote Link to comment https://forums.phpfreaks.com/topic/17263-mysql-connection-problem/#findComment-73221 Share on other sites More sharing options...
corbin Posted August 11, 2006 Share Posted August 11, 2006 Hmmm sorry i thinki misunderstood your question... I thought you meant it wouldnt connect at all... Hmm maybe it doesnt like the 's around users. Try ` instead and then if that doesnt work put or die(mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/17263-mysql-connection-problem/#findComment-73233 Share on other sites More sharing options...
ChaosXero Posted August 11, 2006 Author Share Posted August 11, 2006 It wont output a mysql error when it dies. What does that mean?! Quote Link to comment https://forums.phpfreaks.com/topic/17263-mysql-connection-problem/#findComment-73244 Share on other sites More sharing options...
corbin Posted August 11, 2006 Share Posted August 11, 2006 Try it when its not in a function and mysql_error() should out put an error... Quote Link to comment https://forums.phpfreaks.com/topic/17263-mysql-connection-problem/#findComment-73246 Share on other sites More sharing options...
ChaosXero Posted August 11, 2006 Author Share Posted August 11, 2006 Still no luck... I'm updating the code again with a few changes Quote Link to comment https://forums.phpfreaks.com/topic/17263-mysql-connection-problem/#findComment-73273 Share on other sites More sharing options...
ChaosXero Posted August 11, 2006 Author Share Posted August 11, 2006 BUMP... It's going to be the death of me Quote Link to comment https://forums.phpfreaks.com/topic/17263-mysql-connection-problem/#findComment-73358 Share on other sites More sharing options...
raza.shahzad Posted August 11, 2006 Share Posted August 11, 2006 i really haven't understood your question completely but as an amateur programmer i use to print the variable that i use as a handler for mysql query. like; if you are confused about the step to which the script works you can echo $conn, $db, $result, etc one by one just after the line in which you placed $conn = ...., $db = ...., and so on. this will give you an idea about the step at which your script has a flaw. it is childish( i think it is) but at times it works for me. Quote Link to comment https://forums.phpfreaks.com/topic/17263-mysql-connection-problem/#findComment-73401 Share on other sites More sharing options...
hitman6003 Posted August 11, 2006 Share Posted August 11, 2006 because you are using a function for your login that has a mysql query inside of it, you have to access the db connection from inside the function. There are two ways of doing this:1. Pass the db connection to the function the same as you do the username and password.2. use "global conn;" inside your function so it knows to use the connection from above it.[code]unction login($u, $p) { global conn; $sql = "SELECT * FROM users WHERE username = '".$u."'"; $result = mysql_query($sql, $conn) or die("Error!"); ............[/code] $res = mysql_fetch_assoc($result); Quote Link to comment https://forums.phpfreaks.com/topic/17263-mysql-connection-problem/#findComment-73425 Share on other sites More sharing options...
ChaosXero Posted August 12, 2006 Author Share Posted August 12, 2006 AH! Thank you! It finally works. I was thinking it might have been a scope issue but that never occured to me. Quote Link to comment https://forums.phpfreaks.com/topic/17263-mysql-connection-problem/#findComment-73450 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.