Jump to content

Help with Mysql, PHP with basic username authentication


michaelkane

Recommended Posts

Hi guys how are you doing,

 

I am unable to execute very basic php script connecting with mysql. being a noob I just need a quick fix thats all..

 

here is the code:

<?php session_start(); ?>
<html>

<body>
<form action='index.php?login=yes' method=POST>
Username: <input type=text name='user'><br/>
Password: <input type=password name='pass'><br/>
<input type=submit value='SignIn!'>
</form>

<?php

     $user=$_POST['user'];
     $pass=$_POST['pass'];
     $login=$_GET['login'];
     
     if($login=='yes'){
         $con=mysql_connect('localhost','root','');
         mysql_select_db('test');

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


         mysql_close($con);

         if($result!=1) echo "Login failure";
         else{
              echo "Login success !";
              $_SESSION['user']=$user;

            };
         };

?>

<body>
</html>

 

error I am getting is:

Warning: mysql_result() expects parameter 1 to be resource, boolean given in C:\roooooot\xampp\htdocs\index.php on line 22

Login failure

 

 

 

1. I am assuming root and '' pass is to login mysql.

2. test is the database

3. 'database' is the table where it is checking user/pass

 

the rest is straightforward.

 

can anyone explain what could be wrong. getting same error with a nonexisting database.

 

Thank you!

In your line: $result=mysql_result($get,0);

 

You lack one parameter and that is the column/field name. You should have

$result=mysql_result($get,0,fieldname_here);

But looking deeper in your code, your sql is wrong because you did not put an alias to your count(id). You should have like this

 

$get=mysql_query("SELECT count(id) as howmany FROM database WHERE user='$user' and pass='$pass'");
$result=mysql_result($get,0,"howmany");

 

I hope that helps.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.