PHP + MySQL + CodeIgniter : Check if current row exists

标签 php mysql codeigniter codeigniter-2

我想稍微优化一下脚本并节省CPU一些工作,所以我必须创建for循环。在此循环中,我将使用数据库中的一些数据,下面是我当前的代码:

$result = $this->Model->function();
for ($i = 0; $i < $result->num_rows(); $i++)
{
    echo $result->row_array($i)['row'];
}

这里我需要的是检查下一行是否存在。这一段应该位于 if 语句中 for 循环中所有代码的顶部,但是存在一些问题。如果我在 for 循环中输入 $result->num_rows()+1 并回显最后一行(不存在),则它的值不为 null,但与 row 相同之前。

如何检查当前行是否为空?

最佳答案

或者不必每次都检查 bool 值,而且您不会再次调用 num_rows 方法:

$result = $this->Model->function();
$y = $result->num_rows();
for ($i = 0; $i < $y-1; $i++)
{        
    echo $result->row_array($i)['row'];
}
echo 'last row';
echo $result->row_array($y-1)['row'];

关于PHP + MySQL + CodeIgniter : Check if current row exists,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17522058/

相关文章:

php - 为什么 strtotime (' ' ) 和 strtotime ('.' ) 返回时间戳?

mysql - 从 SQL 中的左连接和内连接中减去计数

mysql - INSERT INTO 仅当不存在时 MYSQL

linux - Codeigniter 页面显示 404 页面未找到

php - CI - 帮助插入多个选择表单,每个表单都有多个选择的选项

codeigniter - 代码点火器使用 URL 错误将变量传递给 Controller

php - 我在 MySQL 中存储 token 时遇到困难

php - PDOException' 消息为 'There is no active transaction'

php - 数据表的组合框 MySQL 过滤器

php - 如何在mysql和php表中添加多个注册表