php - codeigniter 中的 mysql_fetch_row

标签 php mysql codeigniter

如何在codeigniter中使用mysql_fetch_row? 在 php 中,当我用户 mysql_fetch_row 时,我会得到这样的结果

$result = mysql_query("SELECT id,email FROM people WHERE id = '42'");
if (!$result) {
    echo 'Could not run query: ' . mysql_error();
    exit;
}
$row = mysql_fetch_row($result);

echo $row[0]; // 42
echo $row[1]; // the email value

我可以获取带有数字类型索引的数组。

但是当我像这样使用 codeigniter 时

$db=$this->load->database($_SESSION['DB'], TRUE);
        $q=$db->query("select ID, Name from mdashboard");
        foreach($q->result_array() as $d)
        {
            echo $d['ID'];
        }

我得到带有字符串类型索引的数组。

Codeigniter 可以像我的第一个代码一样吗?

最佳答案

在 codeigniter 中使用 mysql get result 作为数组,你可以做到这一点

use result_array() get result  or use row_array() get number
public function getBy($where = array(), $field = '*')
{
    $this->db->select($field);
    $query = $this->db->get_where($this->table_name, $where);
    if (!$query) {
        return array();
    }
    return $query->result_array();
}

关于php - codeigniter 中的 mysql_fetch_row,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42549987/

相关文章:

php - 表单转sql表列

php - Yii 1 : CActiveDataProvider does not return all records

javascript - 学生已存在注册系统验证 (Codeigniter)

javascript - 上传文件时如何将 "progress"和更新事件绑定(bind)到ajax请求

php - 使用 JSON 的 MySQL 查询

java - 哪些 Java 框架的运行方式与 PHP 类似?

php - 如何在没有子类调用的情况下在父类中自动运行构造

php - Doctrine 在多列中搜索字符串

mysql - 不能在 mySQL 中删除外键

php - 在更新时我的 where 条件不起作用