No matter what I try, I keep getting Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource in ___
I tried array also, but here's the code for the login page.
About 23 lines down.
Also, I'm new to PHP, as in, today new.

/* Start session */
if($startSession == TRUE){ session_start();}

/* Config file */
include('config.php');

/* Check for submition */
if($_POST['submitID'] == 1){
	
	/* Connect to database */
	if($connectDatabase == TRUE){$action=TRUE;include('connect.php');}
		
	/* sanitize and check info */
	$userName = mysql_real_escape_string($_POST['userName'],$dbc);
	$password = mysql_real_escape_string($_POST['password'],$dbc);
	
	if($userName == NULL) { $message = 'Please enter username.';}
	if($message == NULL && $password == NULL){ $message = 'Please enter password.';}
	
	if($message == NULL)
	{				
		$userQuery = mysql_fetch_row (mysql_query("SELECT COUNT(*) FROM " . $tableName .
		" WHERE `" . $userNameField . "`='$userName' AND `" . $userPasswordField . "`='$password'"));		
		
		/* If usercount is more than 0 -> ok */
		if($userQuery[0] > 0){
			/* Disconnect from database */
			if($connectDatabase == TRUE){$action=FALSE;include('connect.php');}
	
			$_SESSION['isLoged'] = 'yes';
			$_SESSION['userName'] = $userName;
			
			/* add cookies ?*/
			/* expire in 1 hour */
			if($useCookies == TRUE)
			{
				setcookie("isLoged", 'yes', time()+logedInFor, "/", ".$domainName", 1);
				setcookie("userName", $userName, time()+logedInFor, "/", ".$domainName", 1);
			}

			/* Redirect to login page */
			header("Location: $loginPage");
			exit();
		} else {
			$message = 'Invalid username and/or password!';
		}
	}
	/* Disconnect from database */
	if($connectDatabase == TRUE){$action=FALSE;include('connect.php');}
}
?>

Following these steps will help you avoid php-mysql errors and will clean
up your code a little....

1.create a $query variable (echo, copy and past it in mysql command line)
[If it's not working you have a problem with your query)

2.create a $result variable and store mysql_query($query, $connection); in it.
Create a simple test
if (!$result)
{
die ("Database query failed" . mysql_error());
}

[if you get this error on the screen you have a problem with your connection settings (sql query should be tested in step1)].

3. Use $row = mysql_fetch_row($result);

also, thats not the proper way to check if the
array is empty "if($userQuery[0] > 0)"

try this - " if (isset($userQuery[0])) "
isset will return a bool of true or false

Member Avatar for rajarajan2017

First Check your query retrieving values from the database properly in a seperate file. Write your code in a structured way, avoid nested commands!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.