Page 1 of 1

HELP! needed with PHP login script linked to MySQL database

Posted: Fri Aug 19, 2011 3:30 pm
by RossBryan
Hey peeps can anyone advise me on the problem that I have coming up with one of my PHP login scripts that is used to connect to a MySQL database for login verification. I'm pretty new to PHP and MySQL so sorry for asking for help with for something that is probably going to turn out to be something so silly but i really am stuck.

In the following main code:

Code: Select all

<html>
<body>

<form action="index3.php?login=yes" method="POST">
Username:<input type="text" name="user"><br />
Password:<input type="password" name="pass" ><br/>
<input type="submit" name="login" value="SIGN-IN" ><p>
</form>

<?php

$user=$_POST['user'];
$pass=$_POST['pass'];
$login=$_POST['login'];

echo $login;

function denied()
	{
	echo '<h3><span style= "color:red"> Access Denied!!! </span></h3><br><br>';
	}
	
function granted ($user) 
	{
	echo '<h3><span style= "color:green"> Access Granted!!! </span></h3>';
	echo 'Welcome, ' . $user; 
	}

	if($login=='SIGN-IN')
	{
		$con= include_once "mysql_connect.php";
		
		
		$get = mysql_query("SELECT count(id) FROM login WHERE user='$user' and pass='$pass'");
		$result = mysql_result($get, 0);
	
		if (empty($user) || empty($pass))
		{
		//stop execution of PHP script using die function!
		denied();
		die("<br>Please fill out user login fields carefully....<br>");
		}
		
		if ($result!=1) 
		{
		granted($user);
		}
		
		else
		{
		denied ();
		//$_SESSION['user']=$user;
		}
	}

?>

</body>
</html>
  
Also I have the following external script associated with the above:

Code: Select all

<?php

$db_host = "localhost";
$db_username = "root";
$db_pass = "rhianna";
$db_name = "signin";

@mysql_connect("$db_host", "$db_username", "$db_pass") or die ("Could not connect to MySQL");
@mysql_select_db("$db_name") or die ("No $db_name Database ");

?>
But when run im getting the following message displayed in the browser:
Warning: mysql_result() expects parameter 1 to be resource, string given in C:\xampp\htdocs\index3.php on line 44
I Think the problem lies with in mysql_result() function of the main script when using the $get variable as one of its parameters, but have absolutely no idea what it could be or how it can be fixed:

Code: Select all

$get = mysql_query("SELECT count(id) FROM login WHERE user='$user' and pass='$pass'");
$result = mysql_result($get, 0);
Any ideas??????

Re: HELP! needed with PHP login script linked to MySQL datab

Posted: Fri Aug 19, 2011 3:43 pm
by mikosiko
means that your query is failing

suggested debugging step:
- Separate your query string from the execution and check your raw query to validate if you final query is what do you expect.

Code: Select all

               $query = "SELECT count(id) FROM login WHERE user='$user' and pass='$pass'";
                // echo your raw query
                echo $query;   // and look for any error

                $get = mysql_query($query) or die("Error :" . mysql_error()); // include die() at least as a temporary way to get your errors... use trigger_error() in a production environment ;
                $result = mysql_result($get, 0) or die("Error :" . mysql_error()); // same as before 

Re: HELP! needed with PHP login script linked to MySQL datab

Posted: Fri Aug 19, 2011 3:55 pm
by RossBryan
Thanks for the reply, I modified script to:

Code: Select all

$query = "SELECT count(id) FROM login WHERE user='$user' and pass='$pass'";// echo your raw query
echo $query; // and look for any error

$get = mysql_query($query) or die("Error :" . mysql_error()); // include die() at least as a temporary way to get your errors... use trigger_error() in a production environment ;
$result = mysql_result($get, 0) or die("Error :" . mysql_error()); // same as before
But now im getting the following message:
SIGN-IN SELECT count(id) FROM login WHERE user='RossBryan00' and pass=''Error :Table 'signin.login' doesn't exist

Re: HELP! needed with PHP login script linked to MySQL datab

Posted: Fri Aug 19, 2011 4:09 pm
by mikosiko
SIGN-IN SELECT count(id) FROM login WHERE user='RossBryan00' and pass=''Error :Table 'signin.login' doesn't exist
and which part you don't understand of that message?... lets see:
"SIGN-IN" ... no idea from where you are displaying that is not in the posted code.
SELECT count(id) FROM login WHERE user='RossBryan00' and pass='' this was displayed by the echo that you just included in your code... notice that the $pass is null... that could trigger other problem
Error :Table 'signin.login' doesn't exist ... this is the output of the die() sentence that you added... error message is fairly clear isn't?

Re: HELP! needed with PHP login script linked to MySQL datab

Posted: Fri Aug 19, 2011 4:48 pm
by RossBryan
Thanks i fixed it :rofl:

I ended up modifying the query part as you said but with the following:

Code: Select all

$query = "SELECT id FROM login WHERE user='$user' AND pass='$pass'";
$result = mysql_query($query) or die ("ERROR IN SQL STATEMENT: ".mysql_error());
$row = mysql_fetch_assoc($result);
 
The 'SIGNIN' being displayed part was just an unnecessary echo which I had included, and finally the point in which further demonstrates my inexperience in PHP/MySQL is that all this time I had the sql_connect script incorrectly defined the name if the database in which I wanted access. You wouldn't have known as I didn't provide that script, even so thanks for taking the time to help me out I have still learnt a lot from this....

Re: HELP! needed with PHP login script linked to MySQL datab

Posted: Sat Aug 20, 2011 8:11 am
by phphelpme
We all make mistakes and it can be stressful even though you have the error codes in front of you. Its just exeperience and confidence and that will come in time.

Well done for sorting it out anyway as I expect that gave you a sudden boost in confidence. :)

Best wishes