Jump to content

MySQL Connection Problem...


ChaosXero

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]
Link to comment
https://forums.phpfreaks.com/topic/17263-mysql-connection-problem/
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.
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);

Archived

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

×
×
  • Create New...

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.