Jump to content

Archived

This topic is now archived and is closed to further replies.

ChaosXero

MySQL Connection Problem...

Recommended Posts

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]

Share this post


Link to post
Share on other sites
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');

Share this post


Link to post
Share on other sites
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());

Share this post


Link to post
Share on other sites
Try it when its not in a function and mysql_error() should out put an error...

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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);

Share this post


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

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.