atrum Posted February 23, 2009 Share Posted February 23, 2009 Hello all, I am trying to teach my self php cookies, and php mysql authentication and I am not having much luck with it. When I submit my information on <http://tools.exiled-alliance.com/testsite/index.php>, The following Error is printed to the screen. Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/cdorris/www/tools.exiled-alliance.com/testsite/home.php on line 16 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Select * FROM tbl_auth_user WHERE user_id='tools'' at line 1 Here is the login script: <?php ini_set('display_errors','On'); include("constr.php"); $passwordHash = sha1($_POST['password']); mysql_select_db("tools", $sqlcon); $user ="$_POST[username]"; $sqlu ="SELECT * FROM tbl_auth_user WHERE user_id ='$user'" ; if (!mysql_query($sqlu,$sqlcon)) { die('Error:' . mysql_error()); } else $row = mysql_fetch_array($sqlu); if(!$row['user_password'] = $passwordHash) { die('incorrect user name and or password' . mysql_error()); } mysql_close($sqlcon); include("setcookie.php"); header( 'Location: http://tools.exiled-alliance.com/testsite/home.php' ) ; ?> Here is the setcookie.php: <?php include("constr.php"); mysql_select_db("tools", $sqlcon); $user = $_POST['username']; $sqlu ="Select * FROM tbl_auth_user WHERE user_id='$user'"; setcookie("COOKIE:testsite", "$sqlu", time()+1500); ?> Here is the home.php page which the login script is set to redirect to. <?php ini_set('display_errors','On'); include("constr.php"); mysql_select_db("tools", $sqlcon); //$user1 = $_COOKIE["COOKIE:testsite"]; //$sqlu ="SELECT * FROM tbl_auth_user WHERE user_id ='$user'"; $sqlu ="SELECT * FROM tbl_auth_user WHERE user_id =" . $_COOKIE["COOKIE:testsite"] . ""; if (mysql_query($sqlu,$sqlcon)) { die('Error:' . mysql_error()); } while($row1 = mysql_fetch_array($sqlu)) { echo $row1['firstName']; } echo mysql_error(); ?> Steps to recreate issue: 1. browse to [http://tools.exiled-alliance.com/testsite/index.php] 2. login with. User: testuser, Pass:password I am honestly at wits end with this one, so any help you can provide would be greatly appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/146488-help-getting-error-warning-mysql_fetch_array/ Share on other sites More sharing options...
Mchl Posted February 23, 2009 Share Posted February 23, 2009 Change: if (mysql_query($sqlu,$sqlcon)) to if (!mysql_query($sqlu,$sqlcon)) Quote Link to comment https://forums.phpfreaks.com/topic/146488-help-getting-error-warning-mysql_fetch_array/#findComment-769020 Share on other sites More sharing options...
fenway Posted February 23, 2009 Share Posted February 23, 2009 Also, please stop posting entire scripts... it's not helpful. Quote Link to comment https://forums.phpfreaks.com/topic/146488-help-getting-error-warning-mysql_fetch_array/#findComment-769063 Share on other sites More sharing options...
Mchl Posted February 23, 2009 Share Posted February 23, 2009 Also I believe that in setcookie.php setcookie("COOKIE:testsite", "$sqlu", time()+1500); should be setcookie("COOKIE:testsite", "$user", time()+1500); and in login script $sqlu ="SELECT * FROM tbl_auth_user WHERE user_id =" . $_COOKIE["COOKIE:testsite"] . ""; should be $sqlu ="SELECT * FROM tbl_auth_user WHERE user_id ='{$_COOKIE["COOKIE:testsite"]}'"; Quote Link to comment https://forums.phpfreaks.com/topic/146488-help-getting-error-warning-mysql_fetch_array/#findComment-769087 Share on other sites More sharing options...
atrum Posted February 24, 2009 Author Share Posted February 24, 2009 Hey guys, thanks alot for the help. I think I need to split the different functions up, like the login, the session, and the mysql connection before I try to combine them. The script seems to always fail on the sql results portion under mysql_fetch_array. Does anyone have any suggestions on reading material for what I am trying to accomplish? Quote Link to comment https://forums.phpfreaks.com/topic/146488-help-getting-error-warning-mysql_fetch_array/#findComment-770528 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.