我不明白这一点。我想从数据库中获取一个字符串,并将其添加到另一个字符串中以获取结果。
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');
}
}
错误是这样的:
注意:尝试获取财产&#39; num_rows&#39;第35行中的非对象
答案 0 :(得分:0)
您需要为IN支撑括号!这将纠正您的sql语句。仍然我建议您在传递给sql查询之前是否为$dat
添加检查。
$conn->query("SELECT * FROM logins WHERE subject IN ($dat) LIMIT $start, $limit");
那么最佳做法是明确检查查询结果是对还是错
if ($sql === false) {
exit('reachedMax');
}
//come here if the query is successfully executed
答案 1 :(得分:0)
我以前有同样的问题。
$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) ";
该问题归因于LIMIT。我不知道为什么在我将LIMIT放在那边时会说错误,而当我从查询中删除Limit时错误消失了