0

I have 2 files named login.php and configDb.php . I have a function named sql_query($query) in configDb.php somehow like this:

function sql_query($query) {
$link=mysqli_connect("localhost", "root", "", "basheer");
if (mysqli_connect_error()) {
    echo mysqli_connect_errno();
}
$q=  mysqli_query($link, $query);
while ($row=mysqli_fetch_array($q))
{   
       return $row['username']." ".$row['password'];
}
}

I included file in login.php and I am able to fetch the result in an array variable:

$username = $_POST['user'];//
$password = $_POST['pass'];
$query="select * from tbladmin where username='Admin'";//Sql query
require  'include/config_db.php';
$user=  sql_query($query);//passed the function result to an array var
print_r($user);//prints the result correctly
echo '<br/>';
if ($username == $user['username'] && $password == $user['password'])
//I am stuck here with $user['username] and $user['password'] 
{
   header("location: index.php");
} else {
    $message = "Invalid user name or password, please try again!";
}

How to compare $username and $password with $user array variable ?

4 Answers 4

2

Make your function return an array instead of a string

Change:

return $row['username']." ".$row['password'];

To:

return array("username" => $row['username'], "password" => $row['password']);
Sign up to request clarification or add additional context in comments.

1 Comment

I agree with this answer and would like to add that as long as you get one record returned you do not have to use a while loop to fetch the record.
1

you must return an array : return $row; and not $row['username']." ".$row['password']; it's a string

Comments

0
<?php
    session_start();
    require_once('connectie.php');

    if(isset($_POST['submit'])){
        $username = strip_tags($_POST['username']);
        $password = strip_tags($_POST['password']);

        $username = $db->real_escape_string($username);
        $password = $db->real_escape_string($password);

      $sql = "SELECT id, username, password FROM gebruikers WHERE username = '$username' AND password = '$password'";

      $result = $db->query($sql);

      if($result->num_rows == 1){
        $row = $result->fetch_object();
        $_SESSION['loggedin'] = true;
        $_SESSION['id'] = $row->id;
        $_SESSION['username'] = $row->username;

        print_r($result);
        header('location: ../index.php ');

      }else{
        session_destroy();
        echo "NOPE";
      }
    }
?>

This is what i'm using in my login system.

Comments

0

Best choice is to compare your POST parameter with the username and password exist in database and get the count. If count is 1 its mean authentication success. else return 0

$query = "select count(*) from tbname where username = '".$_POST['username']."' and password = '".$_POST['password']."'";
$run = mysqli_query($query);
$result = mysqli_fetch_array($run);
if($result['count']==1){
    header("location: index.php");
}else{
    $message = "Invalid user name or password, please try again!";
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.