Jump to content

Help with Mysql, PHP with basic username authentication


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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.