This question already has an answer here:
I don't understand this. I want to get a string from database and add it to another string to fetch results.
if (isset($_POST['key'])) {
$conn = new mysqli('localhost', 'root', '', 'ams');
if ($_POST['key'] == 'getStudentsData') {
$start = $conn->real_escape_string($_POST['start']);
$limit = $conn->real_escape_string($_POST['limit']);
$set = $conn->query("SELECT * from teachers where id =$uid");
$dat = $set->fetch_array();
$dat = join(', ', $dat);
$sql = $conn->query("SELECT * FROM logins WHERE subject IN $dat LIMIT $start, $limit");
if (!empty($sql) && $sql->num_rows > 0) {
$response = "";
while ( $data = $sql->fetch_array(MYSQLI_ASSOC)) {
$response .= '
<tr class="text-center">
<td>' . $data["fname"] . ' ' . $data["lname"] . '</td>
<td>' . $data["matno"] . '</td>
<td>' . $data["email"] . '</td>
<td>' . $data["phone_no"] . '</td>
<td class="text-center">
<button value="Edit" class="btn btn-primary"> ' . $user->i('upload') . '</button>
<button value="View" class="btn">' . $user->i('eye') . '</button>
<button value="Delete" class="btn btn-danger">' . $user->i('trash') . '</button>
</td>
</tr>
';
}
exit($response);
} else
exit('reachedMax');
}
}
Error is this:
Notice: Trying to get property 'num_rows' of non-object in line 35
</div>
You need braces for the IN! which will correct your sql statement. Still i would suggest you to add a check for the $dat
before pass to the sql query whether it is not empty.
$conn->query("SELECT * FROM logins WHERE subject IN ($dat) LIMIT $start, $limit");
then best practice is check your query result is true or false explicitly
if ($sql === false) {
exit('reachedMax');
}
//come here if the query is successfully executed
I had the same problem previously.
$q1 = "SELECT * ".
"FROM admin, student ".
"WHERE admin.pcode = student.code AND admin.puser_name = student.puser_name AND admin.id IN (SELECT admin.id FROM admin ORDER BY admin.id DESC LIMIT 1) ";
The problem is due to LIMIT. I don't know why it says error when I put LIMIT over there and the error disappears when I remove Limit from the query