PHP使用函数查找学生记录

标签 php mysql

  • 表:学生
    enter image description here

  • 学生.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/

相关文章:

php - 面向对象 PHP 中的 $this 变量和变量作用域

php - 通过 Composer 更新 symfony 3.2.4 错误

php - SQL查询优化建议

mysql - 如何通过另一个表检查 "duplicates"?

java - 我们如何使用JAVA在MySQL中添加unicode文本

SQL JOIN 以查找没有具有特定值的匹配记录的记录

php - 向 PNG 的非透明部分添加颜色叠加层

php - 更改 mysqli_result 中的行顺序

MySQL:在运行时为客户端确定max_allowed_pa​​cket的值

php - PDO查看查询的插入详细信息