Lethal323 Posted June 23, 2008 Share Posted June 23, 2008 Alright well im making something and when I try to load something from my DB the result comes back as 0 which is also null. I have been working on this for around 2 days and cannot get it to work. Here is my code: <?php session_start(); if (isset($_POST['username']) && isset($_POST['password'])) { $userId_var = $_POST['username']; $password_var = $_POST['password']; $sql = "SELECT user_id FROM users WHERE user_id = '$userId_var' AND user_password = PASSWORD('$password_var')"; $avatar = mysql_query("SELECT user_avatar FROM users WHERE user_id = '.$userId_var.'"); $result = mysql_query($sql) or die('Query failed. ' . mysql_error()); if (mysql_num_rows($result) == 1) { $_SESSION['db_is_logged_in'] = true; $_SESSION['username'] = $userId_var; $_SESSION['avatar'] = $avatar; header('Location: index.php'); } else { $error = 1; $errorMessage = 'Sorry, wrong user id / password'; } } The avatar is what will not load. The username as you can see there. It does load but the avatar doesnt. Quote Link to comment Share on other sites More sharing options...
DarkWater Posted June 23, 2008 Share Posted June 23, 2008 You never actually fetch the result....you need to call mysql_fetch_array() or another mysql_fetch_* function to actually GET the data. Quote Link to comment Share on other sites More sharing options...
Lethal323 Posted June 23, 2008 Author Share Posted June 23, 2008 Alright well I tried this. <?php session_start(); if (isset($_POST['username']) && isset($_POST['password'])) { $userId_var = $_POST['username']; $password_var = $_POST['password']; $sql = "SELECT user_id FROM users WHERE user_id = '$userId_var' AND user_password = PASSWORD('$password_var')"; $avatar = mysql_query("SELECT user_avatar FROM users WHERE user_id = '.$userId_var.'"); $result = mysql_query($sql) or die('Query failed. ' . mysql_error()); if (mysql_num_rows($result) == 1) { $_SESSION['db_is_logged_in'] = true; $_SESSION['username'] = $userId_var; $_SESSION['avatar'] = mysql_fetch_array($avatar); header('Location: index.php'); } else { $error = 1; $errorMessage = 'Sorry, wrong user id / password'; } } The result came back as nothing not a zero or anything. BTW: the result is suppoes to be '<img src=imgs/avatars/dizzy-avatar.jpg>' Quote Link to comment Share on other sites More sharing options...
redarrow Posted June 23, 2008 Share Posted June 23, 2008 Trie this mate might work if not i am sure a user will tell me why.... might all be wrong theo always backup......... <?php session_start(); if (isset($_POST['username']) && isset($_POST['password'])) { $userId_var = $_POST['username']; $password_var = $_POST['password']; $sql = "SELECT user_id FROM users WHERE user_id = '$userId_var' AND user_password = PASSWORD('$password_var')"; $avatar = mysql_query("SELECT user_avatar FROM users WHERE user_id = '.$userId_var.'"); $result = mysql_query($sql) or die('Query failed. ' . mysql_error()); while($x=mysql_fetch_assoc($result)) { $_SESSION['db_is_logged_in'] = true; $_SESSION['username'] = $x['userId_var']; $_SESSION['avatar'] = $x['avatar']; header('Location: index.php'); } else { $error = 1; $errorMessage = 'Sorry, wrong user id / password'; } } ?> Quote Link to comment Share on other sites More sharing options...
redarrow Posted June 23, 2008 Share Posted June 23, 2008 sorry you need a join on my code sorry mate.......... Quote Link to comment Share on other sites More sharing options...
Lethal323 Posted June 23, 2008 Author Share Posted June 23, 2008 Syntax error... I dont have php errors turned on otherwise I would tell you where. Quote Link to comment Share on other sites More sharing options...
DarkWater Posted June 23, 2008 Share Posted June 23, 2008 No no no. D: He's only fetching one result. The while loop is overkill. <?php session_start(); if (isset($_POST['username']) && isset($_POST['password'])) { $userId_var = $_POST['username']; $password_var = $_POST['password']; $sql = "SELECT user_id FROM users WHERE user_id = '$userId_var' AND user_password = PASSWORD('$password_var')"; $avatarrows = mysql_query("SELECT user_avatar FROM users WHERE user_id = '.$userId_var.'"; $result = mysql_query($sql) or die('Query failed. ' . mysql_error()); $avatar = mysql_fetch_assoc($avatarrows); if (mysql_num_rows($result) == 1) { $_SESSION['db_is_logged_in'] = true; $_SESSION['username'] = $userId_var; $_SESSION['avatar'] = $avatar['user_avatar']; header('Location: index.php'); } else { $error = 1; $errorMessage = 'Sorry, wrong user id / password'; } } Quote Link to comment Share on other sites More sharing options...
Lethal323 Posted June 23, 2008 Author Share Posted June 23, 2008 You had a syntax error to. Yes I did get the missing ?> at the end. Quote Link to comment Share on other sites More sharing options...
redarrow Posted June 23, 2008 Share Posted June 23, 2008 ok sorry but understand thank you........... forget that what error your helped but also need you to help your self, while using the forum m8........ Quote Link to comment Share on other sites More sharing options...
DarkWater Posted June 23, 2008 Share Posted June 23, 2008 My bad. $avatarrows = mysql_query("SELECT user_avatar FROM users WHERE user_id = '.$userId_var.'"); Fix that line and make it that. I accidentally left off the ). Quote Link to comment Share on other sites More sharing options...
Lethal323 Posted June 23, 2008 Author Share Posted June 23, 2008 No that wasnt the problem. Quote Link to comment Share on other sites More sharing options...
DarkWater Posted June 23, 2008 Share Posted June 23, 2008 Uhh...well, there are no syntax errors in the code after you correct that. Quote Link to comment Share on other sites More sharing options...
Lethal323 Posted June 23, 2008 Author Share Posted June 23, 2008 I found another problem here is the updated code: <?php session_start(); if (isset($_POST['username']) && isset($_POST['password'])) { $userId_var = $_POST['username']; $password_var = $_POST['password']; $sql = "SELECT user_id FROM users WHERE user_id = '$userId_var' AND user_password = PASSWORD('$password_var')"; $avatarrows = mysql_query("SELECT * FROM users WHERE user_id = '.$userId_var.'"); $result = mysql_query($sql) or die('Query failed. ' . mysql_error()); $avatar = mysql_fetch_assoc($avatarrows); if (mysql_num_rows($result) == 1) { $_SESSION['db_is_logged_in'] = true; $_SESSION['username'] = $userId_var; $_SESSION['avatar'] = $avatar['user_avatar']; header('Location: index.php'); } else { $error = 1; $errorMessage = 'Sorry, wrong user id / password'; } } ?> It still has a syntax error.. Here is the website if you want to test it... www.thecsclan.net Username: Dizzy Password: Kev1b00 Go to edit profile, and notice how it doesnt load the avatar image. Quote Link to comment Share on other sites More sharing options...
DarkWater Posted June 23, 2008 Share Posted June 23, 2008 Here: $avatarrows = mysql_query("SELECT * FROM users WHERE user_id = $userId_var") OR die(mysql_error()); You didn't end the string before concatenation. Not a PHP error, just a MySQL one. And since you didn't error check....it didn't work. =/ Now it should. Quote Link to comment Share on other sites More sharing options...
Lethal323 Posted June 23, 2008 Author Share Posted June 23, 2008 Came back with this Unknown column 'Dizzy' in 'where clause' While trying to login... Quote Link to comment Share on other sites More sharing options...
DarkWater Posted June 23, 2008 Share Posted June 23, 2008 Wait, what the hell? Is user_id the user id or the username? This is why you plan PHP projects before you start. $avatarrows = mysql_query("SELECT * FROM users WHERE user_id = '$userId_var'") OR die(mysql_error()); Quote Link to comment Share on other sites More sharing options...
Lethal323 Posted June 23, 2008 Author Share Posted June 23, 2008 I did plan this before I did it. user_id = username I dont use user_id's Quote Link to comment Share on other sites More sharing options...
DarkWater Posted June 23, 2008 Share Posted June 23, 2008 Then you have no primary and foreign key, which means your database is going to end up failing miserably if you wanted to add anything to it. =/ Anyway, use the new line I posted. Quote Link to comment Share on other sites More sharing options...
Lethal323 Posted June 23, 2008 Author Share Posted June 23, 2008 I love you guys. Thanks for all your help. Quote Link to comment Share on other sites More sharing options...
DarkWater Posted June 23, 2008 Share Posted June 23, 2008 No problem. You should really rework your database though. =X Quote Link to comment 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.