<?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>';
}
?>
- 356,200
- 43
- 426
- 500
- 25
- 2
-
3your query failed, you failed to check for failure, and are now trying to use the boolean `false` that was returned to TELL YOU about the failure as if it was an object. In other words, never **EVER** assume success with db operations (or using any external resource). always assume failure, check for failure, and treat success as a pleasant surprise. – Marc B Jan 26 '16 at 20:18
-
1If your query returns nothing, $conn->query($query) will return false. – Chin Leung Jan 26 '16 at 20:20
-
thx but can u help me pls – ASAP Gaming Jan 26 '16 at 20:21
-
how do i fix this KayVan – ASAP Gaming Jan 26 '16 at 20:22
-
what does `conn.php` look like? – Martin Jan 26 '16 at 20:28
-
my conn.php – ASAP Gaming Jan 26 '16 at 20:29
-
2Possible duplicate of [Reference - What does this error mean in PHP?](http://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php) – Jay Blanchard Jan 26 '16 at 20:31
-
possible duplicate of "I need to learn how to problem solve" – Martin Jan 26 '16 at 20:32
-
tha guys for your help if fix it thx thx everybody – ASAP Gaming Jan 26 '16 at 20:38
2 Answers
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.
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>';
}
?>
- 3,301
- 8
- 22
- 27
- 25
- 2