学生.php
<?php function findStudentRecord() { //Db connection $q1 = "select * from student where gender = 'F'"; $r1 = mysqli_query($dbc, $q1); $total_records = mysqli_num_rows($r1); $record = array(); if($total_records > 0) { while($row1 = mysqli_fetch_array($r1, MYSQLI_ASSOC)) { $record[] = $row1; } } else { //[HERE] } return $record; } $record = findStudentRecord(); //[HERE] ?>
我想查找女学生记录,但我的数据库中没有记录。如何从函数返回 0 值并在网页的 [HERE] 部分显示“未找到记录”?
最佳答案
if($total_records > 0)
不添加其他 block ,所以它会返回一个空数组,现在你可以做这样的事情
$records = findStudentRecord();
if(count($records) === 0) {
echo "No record found";
}
关于PHP使用函数查找学生记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33629651/