Help debugging login script?

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
Cucumberdude
Forum Newbie
Posts: 14
Joined: Sun Dec 12, 2010 11:53 pm

Help debugging login script?

Post by Cucumberdude »

Code: Select all

<?php
	//Retrieve form data
	$username = $_POST['username'];
	$password = $_POST['password'];
	
	//Access mySQL database to check the pass and user match
	$mySQL_password = "PASSWORD";
	$mySQL_username = "USERNAME";
	$mySQL_localhost = "HOST";
	
	$cnx = mysqli_connect($mySQL_localhost, $mySQL_username, $mySQL_password)
		or die("Error: could not connect to database!");
	
	$database = "sterli41_g4j";
	
	$dbselect = mysqli_select_db($cnx, $database)
		or die("Error: could not select database!");
		
	$query = "SELECT * FROM members";
	
	$result = mysqli_query($cnx, $query)
		or die(mySQL_error());
	
	$login = false;
	
	while ($row = mysql_fetch_array($result))
	{
		$pass = $row['password'];
		$user = $row['username'];
		
		if($user == $username)
		{
			if($pass == $password)
			{
				$login = true;
			}
		}
	}


	if ($login)
	{
		echo("LOGIN NOW");
	}
	else
	{
		echo("LOGIN FAILED!");
	}
	
	mysqli_close($cnx);
		
?>
That's the code for a login script. I get this:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in gamesforjunk/logincheck.php on line 26
LOGIN FAILED!
each time I try to log in, even if I use the correct pass/user combo.

Where am I going wrong?
User avatar
Technical
Forum Commoner
Posts: 81
Joined: Thu Dec 02, 2010 5:30 am

Re: Help debugging login script?

Post by Technical »

Because you're using MySQLi, you need mysqli_fetch_array() function.
Cucumberdude
Forum Newbie
Posts: 14
Joined: Sun Dec 12, 2010 11:53 pm

Re: Help debugging login script?

Post by Cucumberdude »

I feel dumb now >.<
User avatar
Technical
Forum Commoner
Posts: 81
Joined: Thu Dec 02, 2010 5:30 am

Re: Help debugging login script?

Post by Technical »

No pain, no gain
Post Reply