Basheer Kharoti
Basheer Kharoti

Reputation: 4302

php function return the result into an array variable

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 ?

Upvotes: 0

Views: 912

Answers (4)

Waqas Ghouri
Waqas Ghouri

Reputation: 1150

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!";
}

Upvotes: 0

AddcitedToLearn
AddcitedToLearn

Reputation: 398

<?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.

Upvotes: 0

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

Upvotes: 1

Ali
Ali

Reputation: 3461

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']);

Upvotes: 2

Related Questions