alexcrosson Posted October 1, 2006 Share Posted October 1, 2006 Alright so i'm working on a simple login script and when I test the script heres the error i get :[b]Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/totallyc/public_html/login/index.php on line 27[/b]And my code for line 27 is :[code] if (mysql_num_rows($result) != 0) { /* This line is line 27 */ //start the session and register a variable session_start(); session_register('username'); //successful login code will go here... $error = "Success";[/code]Any suggestions? Quote Link to comment https://forums.phpfreaks.com/topic/22664-mysql-error-help-please/ Share on other sites More sharing options...
Daniel0 Posted October 1, 2006 Share Posted October 1, 2006 We would need to see the part where $result is set/defined. Quote Link to comment https://forums.phpfreaks.com/topic/22664-mysql-error-help-please/#findComment-101883 Share on other sites More sharing options...
Balmung-San Posted October 1, 2006 Share Posted October 1, 2006 Yes, post your query. It would also be good to tell you that session_start() needs to be the first thing in your script. Quote Link to comment https://forums.phpfreaks.com/topic/22664-mysql-error-help-please/#findComment-101884 Share on other sites More sharing options...
HuggieBear Posted October 1, 2006 Share Posted October 1, 2006 [quote author=Balmung-San link=topic=110150.msg444763#msg444763 date=1159721726]It would also be good to tell you that session_start() needs to be the first thing in your script.[/quote]That's not entirely true, but it has to be specified before anything is output to the browser. I think it's good practice to put it at the top of the script though and you're more likely to remember it that way.As for the problem, you'll probably find that mysql_query() returned false rather than true, so as previously stated, your SQL is probably not quite right. What's the statement you're using?RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/22664-mysql-error-help-please/#findComment-101892 Share on other sites More sharing options...
alexcrosson Posted October 1, 2006 Author Share Posted October 1, 2006 [code]<?php//check that the user is calling the page from the login form and not accessing it directly//and redirect back to the login form if necessary//check that the form fields are not empty, and redirect back to the login page if they areif (isset($_POST['Login'])) { if (empty($_POST['username']) || empty($_POST['password'])) { $result = "Username or Password field appear to be empty."; } else { // Convert Posted Data to Variables $user = $_POST['username']; $pass = $_POST['password']; $dbHost = "localhost"; $dbUser = "**"; $dbPass = "**"; $dbDatabase = "****"; // Connect to Database $conn = mysql_connect($dbHost,$dbUser,$dbPass) or die ("Unable to connect to Database"); mysql_select_db($dbDatabase); $query = mysql_query("SELECT * FROM users WHERE username='$user' AND password='$pass'"); $result = mysql_fetch_array($query); //check that at least one row was returned if (mysql_num_rows($result) != 0) { //start the session and register a variable session_start(); session_register('username'); //successful login code will go here... $error = "Success"; //we will redirect the user to another page where we will make sure they're logged in //header( "Location: checkLogin.php" ); } else { //if nothing is returned by the query, unsuccessful login code goes here... $error = 'Incorrect login name or password. Please try again.'; } }}?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/22664-mysql-error-help-please/#findComment-101894 Share on other sites More sharing options...
HuggieBear Posted October 1, 2006 Share Posted October 1, 2006 ok, change this line:[code=php:0]$query = mysql_query("SELECT * FROM users WHERE username='$user' AND password='$pass'");[/code]To this:[code=php:0]$query = mysql_query("SELECT * FROM users WHERE username='$user' AND password='$pass'") or die ("Can't execute: " . mysql_error());[/code]And see what you get.RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/22664-mysql-error-help-please/#findComment-101900 Share on other sites More sharing options...
wildteen88 Posted October 1, 2006 Share Posted October 1, 2006 As you ar using a MySQL keyword for a field name in your users table. You'll want to use the following as the query:[code]SELECT * FROM users WHERE `username`='$user' AND `password`='$pass'[/code] Quote Link to comment https://forums.phpfreaks.com/topic/22664-mysql-error-help-please/#findComment-101901 Share on other sites More sharing options...
alexcrosson Posted October 1, 2006 Author Share Posted October 1, 2006 same error :[b]Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/totallyc/public_html/login/index.php on line 27[/b] Quote Link to comment https://forums.phpfreaks.com/topic/22664-mysql-error-help-please/#findComment-101903 Share on other sites More sharing options...
wildteen88 Posted October 1, 2006 Share Posted October 1, 2006 Lookimg at your code. You want to use $query rather than $result on this line:[code=php:0]if (mysql_num_rows($result) != 0) {[/code]mysql_num_rows requires the result resource from mysql_query. Not from mysql_fetch_array. mysql_fetch_array returns the results from the query into an array.OS the above line should be this:[code=php:0]if (mysql_num_rows($query) != 0) {[/code] Quote Link to comment https://forums.phpfreaks.com/topic/22664-mysql-error-help-please/#findComment-101904 Share on other sites More sharing options...
alexcrosson Posted October 1, 2006 Author Share Posted October 1, 2006 wow that did it . thanks wildteen and huggie and all others that helped ;D Quote Link to comment https://forums.phpfreaks.com/topic/22664-mysql-error-help-please/#findComment-101908 Share on other sites More sharing options...
HuggieBear Posted October 1, 2006 Share Posted October 1, 2006 I didn't even notice that :(Huggie Quote Link to comment https://forums.phpfreaks.com/topic/22664-mysql-error-help-please/#findComment-101909 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.