Jump to content


Photo

MySQL Connection Problem...


  • Please log in to reply
10 replies to this topic

#1 ChaosXero

ChaosXero
  • Members
  • PipPipPip
  • Advanced Member
  • 80 posts

Posted 11 August 2006 - 05:18 PM

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)

<?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";
			}
		}
	}

		
?>



#2 corbin

corbin
  • Staff Alumni
  • Advanced Member
  • 8,129 posts

Posted 11 August 2006 - 05:36 PM

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');
Why doesn't anyone ever say hi, hey, or whad up world?

#3 ChaosXero

ChaosXero
  • Members
  • PipPipPip
  • Advanced Member
  • 80 posts

Posted 11 August 2006 - 05:39 PM

Hmm... it works like that on the other pages.  I'll try it though just in case.

No Luck.

#4 corbin

corbin
  • Staff Alumni
  • Advanced Member
  • 8,129 posts

Posted 11 August 2006 - 05:49 PM

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());
Why doesn't anyone ever say hi, hey, or whad up world?

#5 ChaosXero

ChaosXero
  • Members
  • PipPipPip
  • Advanced Member
  • 80 posts

Posted 11 August 2006 - 05:56 PM

It wont output a mysql error when it dies.  What does that mean?!

#6 corbin

corbin
  • Staff Alumni
  • Advanced Member
  • 8,129 posts

Posted 11 August 2006 - 05:57 PM

Try it when its not in a function and mysql_error() should out put an error...
Why doesn't anyone ever say hi, hey, or whad up world?

#7 ChaosXero

ChaosXero
  • Members
  • PipPipPip
  • Advanced Member
  • 80 posts

Posted 11 August 2006 - 06:37 PM

Still no luck... I'm updating the code again with a few changes


#8 ChaosXero

ChaosXero
  • Members
  • PipPipPip
  • Advanced Member
  • 80 posts

Posted 11 August 2006 - 09:53 PM

BUMP... It's going to be the death of me


#9 raza.shahzad

raza.shahzad
  • Members
  • PipPip
  • Member
  • 23 posts

Posted 11 August 2006 - 11:01 PM

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.

#10 hitman6003

hitman6003
  • Members
  • PipPipPip
  • Advanced Member
  • 1,807 posts

Posted 11 August 2006 - 11:29 PM

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.

unction login($u, $p) {
	global conn;
	$sql = "SELECT * FROM users WHERE username = '".$u."'";
	$result = mysql_query($sql, $conn) or die("Error!");
	............

$res = mysql_fetch_assoc($result);

#11 ChaosXero

ChaosXero
  • Members
  • PipPipPip
  • Advanced Member
  • 80 posts

Posted 12 August 2006 - 12:15 AM

AH! Thank you! It finally works.  I was thinking it might have been a scope issue but that never occured to me.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users