ma5ect Posted June 25, 2008 Share Posted June 25, 2008 Hi, I 'am trying to connect my sql database with my webpage for users log in. i have got this script so far but i keep getting the following error message which i cannot figure out.. could any1 help.. error:Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\checklogin.php on line 18 Wrong Username or Password code: <? $hostname_mysql_connect = "localhost"; $database_mysql_connect = "website_members"; $username_mysql_connect = "root"; $password_mysql_connect = "******"; $tblname_mysql_connect ="members"; $mysql_connect = mysql_pconnect($hostname_mysql_connect, $username_mysql_connect, $password_mysql_connect, $tblname_mysql_connect) or trigger_error(mysql_error(),E_USER_ERROR); mysql_select_db($database_mysql_connect) or die(mysql_error()); $myusername = $_POST['myusername']; $mypassword = $_POST['mypassword']; $sql = "SELECT * FROM $tblname_mysql_connect WHERE username= '$myusername' and password='$mypassword'"; $result = mysql_query($sql); $count = mysql_num_rows($result); (line 18) if($count==1) { session_register("myusername"); session_register("mypassword"); header("location:login_success.php"); } else { echo "Wrong Username or Password"; } ?> many thanx Link to comment https://forums.phpfreaks.com/topic/111838-solved-php-help_connecting-sql-database/ Share on other sites More sharing options...
timmah1 Posted June 25, 2008 Share Posted June 25, 2008 try this instead of that tutorial your form <form action="<?php echo $SCRIPT_NAME; ?>" method="POST"> <table> <tr> <td>Email:</td> <td><input type="textbox" name="email"> </tr> <tr> <td>Password:</td> <td><input type="password" name="password"> </tr> <tr> <td></td> <td><input type="submit" name="login" value="Log In"> </tr> </table> </form> then the code to process the form $dbhost = "localhost"; $dbuser = "USERNAME"; $dbpassword = "PASSWORD"; $dbdatabase = "DATABASE; $db = mysql_connect($dbhost, $dbuser, $dbpassword); mysql_select_db($dbdatabase, $db); $config_sitename = "YOURSITENAME"; $config_basedir = "PATH TO YOUR SITE"; $loginsql = "SELECT * FROM members WHERE username= '" . $_POST['username'] . "' AND password = '" . $_POST['password'] . "'"; $loginres = mysql_query($loginsql); $numrows = mysql_num_rows($loginres); if($numrows == 1) { $loginrow = mysql_fetch_assoc($loginres); session_register("SESS_LOGGEDIN"); session_register("SESS_USER"); session_register("SESS_COMPANY"); session_register("SESS_USERID"); $_SESSION['SESS_LOGGEDIN'] = 1; $_SESSION['SESS_USER'] = $loginrow['username']; $_SESSION['SESS_COMPANY'] = $loginrow['company']; $_SESSION['SESS_USERID'] = $loginrow['user_id']; header("Location: " . $config_basedir); } else { header("Location: http://" . $HTTP_HOST . $SCRIPT_NAME . "?error=incorrect"); } switch($_GET['error']) { case "incorrect": echo "<font color=#FF0000><strong>Incorrect email/password</strong></font>"; break; } Link to comment https://forums.phpfreaks.com/topic/111838-solved-php-help_connecting-sql-database/#findComment-574173 Share on other sites More sharing options...
PFMaBiSmAd Posted June 25, 2008 Share Posted June 25, 2008 The error means the mysql_query() failed. Since there is no error checking of the result of the mysql_query(), the code blindly continues execution and attempts to access a non-existent result resource. The original code has error checking on the pconnect and select database functions but it has no error checking on the mysql_query(). The examples in the php manual under mysql_query() show basic error checking that will get your code to tell you why the query failed. @timmah1, the code you posted has absolutely no error checking in it and it will fail for same reason as the original code. Link to comment https://forums.phpfreaks.com/topic/111838-solved-php-help_connecting-sql-database/#findComment-574194 Share on other sites More sharing options...
ma5ect Posted June 25, 2008 Author Share Posted June 25, 2008 thanx 4replies. But i dont understand da debuggin process. I am pretty new to php code Link to comment https://forums.phpfreaks.com/topic/111838-solved-php-help_connecting-sql-database/#findComment-574383 Share on other sites More sharing options...
.josh Posted June 25, 2008 Share Posted June 25, 2008 http://www.phpfreaks.com/tutorial/debugging-a-beginners-guide Link to comment https://forums.phpfreaks.com/topic/111838-solved-php-help_connecting-sql-database/#findComment-574387 Share on other sites More sharing options...
ma5ect Posted June 26, 2008 Author Share Posted June 26, 2008 http://www.phpfreaks.com/tutorial/debugging-a-beginners-guide Hi, i changed the code in the query to : $sql = "SELECT * FROM $tblname_mysql_connect WHERE username= '$myusername' and password='$mypassword'"; $result = mysql_query($sql) or die(mysql_error()); and this message is shown: Unknown column 'password' in 'where clause' Link to comment https://forums.phpfreaks.com/topic/111838-solved-php-help_connecting-sql-database/#findComment-574893 Share on other sites More sharing options...
waynew Posted June 26, 2008 Share Posted June 26, 2008 Make sure that you named your column password. I often find sometimes that I assumed I've called a table something, when in reality I used something like pass. Link to comment https://forums.phpfreaks.com/topic/111838-solved-php-help_connecting-sql-database/#findComment-574895 Share on other sites More sharing options...
ma5ect Posted June 26, 2008 Author Share Posted June 26, 2008 Make sure that you named your column password. I often find sometimes that I assumed I've called a table something, when in reality I used something like pass. I just figured it out...i mis spelt the password field.. many thanx for all posts.. Link to comment https://forums.phpfreaks.com/topic/111838-solved-php-help_connecting-sql-database/#findComment-574899 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.