jrp91384 Posted January 28, 2008 Share Posted January 28, 2008 Hi, I’m trying to authenticate a user against a MySQL database and get the following errors. Note, I’m a beginner in the early stages and this is script from a book I’m reading. Any help would be greatly appreciated! Thank you… Error: Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in C:\xampp\htdocs\test\passwordtest2.php on line 27 Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\test\passwordtest2.php on line 30 Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\test\passwordtest2.php:27) in C:\xampp\htdocs\test\passwordtest2.php on line 6 Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\test\passwordtest2.php:27) in C:\xampp\htdocs\test\passwordtest2.php on line 7 My Database: authenticationtest My authentication table settings are as follows: rowID TINYINT UNSIGNED NOT NULL AUTO_INCREMENT, commonname VARCHAR(35) NOT NULL, username VARCHAR(9) NOT NULL, pswd VARCHAR(32) NOT NULL, PRIMARY KEY(rowID)); My php code is as follows (with the exception of my username and password): <?php /* Because the authentication prompt needs to be invoked twice, embed it within a function. */ function authenticate_user() { header('WWW-Authenticate: Basic realm="Secret Stash"'); header("HTTP/1.0 401 Unauthorized"); exit; } /* If $_SERVER['PHP_AUTH_USER'] is blank, the user has not yet been prompted for the authentication information. */ if (! isset($_SERVER['PHP_AUTH_USER'])) { authenticate_user(); } else { // Connect to the MySQL database by entering the MySQL server, username, and password. Also enter the db on next line. $conn = mysql_connect("localhost", "myusername", "mypassword"); $db = mysql_select_db("authenticationtest"); // Create and execute the selection query. $query = "SELECT username, pswd FROM userauth WHERE username='$_SERVER[php_AUTH_USER]' AND pswd=md5('$_SERVER[php_AUTH_PW]')"; $result = mysql_query($conn, $query); // If nothing was found, reprompt the user for the login information. if (mysql_num_rows($result) == 0) { authenticate_user(); } else { echo "Welcome to the Authentication Test!"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/88260-authenticating-a-user-against-a-mysql-table/ Share on other sites More sharing options...
hitman6003 Posted January 28, 2008 Share Posted January 28, 2008 Your mysql connection is failing for whatever reason...use error checking: change $result = mysql_query($conn, $query); to $result = mysql_query($conn, $query) or die(mysql_error()); and it will print out the error. Do the same for your mysql_query call. Quote Link to comment https://forums.phpfreaks.com/topic/88260-authenticating-a-user-against-a-mysql-table/#findComment-451655 Share on other sites More sharing options...
revraz Posted January 28, 2008 Share Posted January 28, 2008 Try it with just $result = mysql_query($query) or die (mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/88260-authenticating-a-user-against-a-mysql-table/#findComment-451658 Share on other sites More sharing options...
jrp91384 Posted January 28, 2008 Author Share Posted January 28, 2008 Now when logging in it will not accept the username and password and goes directly to a blank page on the third attempt. Quote Link to comment https://forums.phpfreaks.com/topic/88260-authenticating-a-user-against-a-mysql-table/#findComment-451677 Share on other sites More sharing options...
jrp91384 Posted January 29, 2008 Author Share Posted January 29, 2008 Your mysql connection is failing for whatever reason...use error checking: change $result = mysql_query($conn, $query); to $result = mysql_query($conn, $query) or die(mysql_error()); and it will print out the error. Do the same for your mysql_query call. It gives me error: Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in C:\xampp\htdocs\test\passwordtest2.php on line 27 Sorry, but I'm not sure how to do the same for my mysql_query call? What exactly does that warning mean? Quote Link to comment https://forums.phpfreaks.com/topic/88260-authenticating-a-user-against-a-mysql-table/#findComment-451786 Share on other sites More sharing options...
revraz Posted January 29, 2008 Share Posted January 29, 2008 mysql_query takes $string, $resource and you have it backwards, which is why I posted above the correct way. Quote Link to comment https://forums.phpfreaks.com/topic/88260-authenticating-a-user-against-a-mysql-table/#findComment-451797 Share on other sites More sharing options...
jrp91384 Posted January 29, 2008 Author Share Posted January 29, 2008 mysql_query takes $string, $resource and you have it backwards, which is why I posted above the correct way. I tried: $result = mysql_query($query) or die (mysql_error()); but when logging in it will not accept the username and password and goes directly to a blank page on the third attempt. Any suggestions? Quote Link to comment https://forums.phpfreaks.com/topic/88260-authenticating-a-user-against-a-mysql-table/#findComment-451843 Share on other sites More sharing options...
jrp91384 Posted January 29, 2008 Author Share Posted January 29, 2008 Any further suggestions would be helpful. Thank You! Quote Link to comment https://forums.phpfreaks.com/topic/88260-authenticating-a-user-against-a-mysql-table/#findComment-452703 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.