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. Link to comment https://forums.phpfreaks.com/topic/111418-mysql-select-comes-back-as-null/ 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. Link to comment https://forums.phpfreaks.com/topic/111418-mysql-select-comes-back-as-null/#findComment-571891 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>' Link to comment https://forums.phpfreaks.com/topic/111418-mysql-select-comes-back-as-null/#findComment-571895 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'; } } ?> Link to comment https://forums.phpfreaks.com/topic/111418-mysql-select-comes-back-as-null/#findComment-571900 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.......... Link to comment https://forums.phpfreaks.com/topic/111418-mysql-select-comes-back-as-null/#findComment-571907 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. Link to comment https://forums.phpfreaks.com/topic/111418-mysql-select-comes-back-as-null/#findComment-571908 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'; } } Link to comment https://forums.phpfreaks.com/topic/111418-mysql-select-comes-back-as-null/#findComment-571909 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. Link to comment https://forums.phpfreaks.com/topic/111418-mysql-select-comes-back-as-null/#findComment-571913 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........ Link to comment https://forums.phpfreaks.com/topic/111418-mysql-select-comes-back-as-null/#findComment-571915 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 ). Link to comment https://forums.phpfreaks.com/topic/111418-mysql-select-comes-back-as-null/#findComment-571917 Share on other sites More sharing options...
Lethal323 Posted June 23, 2008 Author Share Posted June 23, 2008 No that wasnt the problem. Link to comment https://forums.phpfreaks.com/topic/111418-mysql-select-comes-back-as-null/#findComment-571919 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. Link to comment https://forums.phpfreaks.com/topic/111418-mysql-select-comes-back-as-null/#findComment-571920 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. Link to comment https://forums.phpfreaks.com/topic/111418-mysql-select-comes-back-as-null/#findComment-571922 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. Link to comment https://forums.phpfreaks.com/topic/111418-mysql-select-comes-back-as-null/#findComment-571924 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... Link to comment https://forums.phpfreaks.com/topic/111418-mysql-select-comes-back-as-null/#findComment-571928 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()); Link to comment https://forums.phpfreaks.com/topic/111418-mysql-select-comes-back-as-null/#findComment-571929 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 Link to comment https://forums.phpfreaks.com/topic/111418-mysql-select-comes-back-as-null/#findComment-571930 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. Link to comment https://forums.phpfreaks.com/topic/111418-mysql-select-comes-back-as-null/#findComment-571931 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. Link to comment https://forums.phpfreaks.com/topic/111418-mysql-select-comes-back-as-null/#findComment-571939 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 Link to comment https://forums.phpfreaks.com/topic/111418-mysql-select-comes-back-as-null/#findComment-571979 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.