我正在使用PHP / MySQLi应用程序,其中有2个表attendance
和students
。
students
包含以下字段:student_id,全名,电话,电子邮件,性别,部门和级别。
attendance
该表包含以下字段:Attenance_id,student_id,department_id,level_id。
我能够根据其部门和级别来提取所有记录在出勤表中的学生。
假设我能够提取所有记录在出勤表中并且在200L(with level_id,2)计算机科学(with department_id,4)部门中的学生,如果出席的学生列表很多且已分页我想参考学生表在出勤表中搜索特定学生的全名。
SQL查询将如何?我尝试了以下无效的查询。
$search_query = mysqli_query($db_connect, "SELECT *FROM attendance WHERE student_id=\"SELECT student_id FROM students WHERE fullname LIKE '%$student_fullname%'\";
请帮忙。
您要查找的SQL查询是:
select * from attendance join students on attendance.student_id = students.student_id where students.fullname like '%NAME%'
因此在PHP中,您将需要以下内容:
$query_string = "select * from attendance join students on attendance.student_id = students.student_id where students.fullname like '%$student_fullname%'";
$search_query = mysqli_query($db_connect, $query_string);
我还是建议您看看准备好的语句,以防止SQL注入:http : //php.net/manual/en/mysqli.prepare.php
/* create a prepared statement */
$query_string = "select * from attendance join students on attendance.student_id = students.student_id where students.fullname like ?";
if ($stmt = $mysqli->prepare($query_string)) {
$stmt->bind_param("s", $student_fullname);
$stmt->execute();
$result = $stmt->get_result();
while ($myrow = $result->fetch_assoc()) {
// use your $myrow array as you would with any other fetch
printf("%s found in attendance record ID: %s\n", $student_fullname, $myrow['attendance_id']);
}
$stmt->close();
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句