-2
<?php

session_start();

include "conn.php";

$username = $conn->real_escape_string($_POST['username']);
$password = $conn->real_escape_string($_POST['password']);

$query = "SELECT * FROM admin WHERE username = '$username' AND password = '$password' ";

$hasil = $conn->query($query);

$hitung = $hasil->num_rows;

if($hitung){

    $cetak = $hasil->fetch_assoc();
    extract($cetak);

    $_SESSION['id_users'] = $id_users;
    echo '<script>window.alert("Welcome you had been logged in");windows.location=("../work/admin.php");</script>';
}
else{
    echo '<script>window.alert("Sorry,username or password is wrong");windows.location("../work/login.php");</script>';
}
?>
Marc B
  • 356,200
  • 43
  • 426
  • 500

2 Answers2

0
12> $hasil = $conn->query($query);
13> 
14> $hitung = $hasil->num_rows;

$hasil is not an object. $hasil is the contents of ->query method part of the $conn object.

Your error:

Trying to get property of non-object in C:\wamp\www\work\p_login_admin.php on line 14

So this should tell you that on line 14, the source item is not an object, yet you are treating it as an object.

Solutions:

1) You almost certainly don't want the contents of the $hasil variable but the original contents of the $conn Object. Without knowing what your $conn object contains or what the class contains or even how your output is expected to be the choices for you depend on these details you have yet to share with us.

However, the details of a solution depend on knowing what exact elements and structures we're working with.

(Also please ensure that your questions contain all the details required that a solution can be presented.)


EDIT:

Viewing Fatal error: Call to undefined function new_mysqli() in C:\wamp\www\work\conn.php on line 8 shows me the start of your conn.php script. So

$hitung = $conn->num_rows;

This should give you the correct result direct from the MySQLi object (started within the conn.php file).

Also be aware that the query data returned to $hasil will NOT be ready to use in PHP because you will still need to run a MySQLi Fetch method on the retrieved data.

Community
  • 1
  • 1
Martin
  • 22,212
  • 11
  • 70
  • 132
0

The mistake was in this line $query = "SELECT * FROM admin WHERE username = '$username' AND password = '$password' ";

It wasn't admin it should be users.

<?php

    session_start();

    include "conn.php";

    $username = $conn->real_escape_string($_POST['username']);
    $password = $conn->real_escape_string($_POST['password']);

    $query = "SELECT * FROM admin WHERE username = '$username' AND password = '$password' ";

    $hasil = $conn->query($query);

    $hitung = $hasil->num_rows;

    if($hitung){

        $cetak = $hasil->fetch_assoc();
        extract($cetak);

        $_SESSION['id_users'] = $id_users;
        echo '<script>window.alert("Welcome you had been logged in");windows.location=("work/admin.php");</script>';
    }
    else{
        echo '<script>window.alert("Sorry,username or password is wrong");windows.location("work/login.php");</script>';
    }
?>
Tristan
  • 3,301
  • 8
  • 22
  • 27