如何在CodeIgniter中使用mysql_fetch_row

时间:2017-12-27 08:11:51

标签: php codeigniter

有人能告诉我如何在CodeIgniter中使用mysql_fetch_row吗?我的代码是这样的:

while($row=mysql_fetch_row($result)){
    for($i=1;$i<=$spasi;$i++){
        echo "|&nbsp;&nbsp;";
    }
    if ($row[8] === 'Koleris') {
        $keputusan = "<font color=green>$row[8]</font>";
    } elseif ($row[8] === 'Melankolis') {
        $keputusan = "<font color=blue>$row[8]</font>";
    } elseif ($row[8] === 'Plegmatis') {
        $keputusan = "<font color=purple>$row[8]</font>";
    } elseif ($row[8] === 'Sanguins') {
        $keputusan = "<font color=red>$row[8]</font>";
    } elseif ($row[8] === '?') {
        $keputusan = "<font color=black>$row[8]</font>";
    } else {
        $keputusan = "<b>$row[8]</b>";
    }
    echo "<font color=red>$row[1]</font> = $row[2] (Koleris = $row[4], Melankolis = $row[5], Plegmatis = $row[6], Sanguins = $row[7]) : <b>$keputusan</b><br>";

    /*panggil dirinya sendiri*/
    get_subfolder($row[0], $spasi + 1);
}

如果我们将在CodeIgniter中使用此代码,代码将是什么?

3 个答案:

答案 0 :(得分:0)

试试这个:

$query = $this->db->query("YOUR QUERY");

$row = $query->row();

if (isset($row))
{
        echo $row->title;
        echo $row->name;
        echo $row->body;
}

答案 1 :(得分:0)

要使用最少的更改保存旧代码,您可以尝试这种方法

 while($row=$query->unbuffered_row()){
    $row = array_values($row);

答案 2 :(得分:0)

您可以简单地写一下: -

$result包含来自查询的多条记录

foreach($result as $row){

    for($i=1;$i<=$spasi;$i++){
        echo "|&nbsp;&nbsp;";
    }
    if ($row[8] === 'Koleris') {
        $keputusan = "<font color=green>$row[8]</font>";
    } elseif ($row[8] === 'Melankolis') {
        $keputusan = "<font color=blue>$row[8]</font>";
    } elseif ($row[8] === 'Plegmatis') {
        $keputusan = "<font color=purple>$row[8]</font>";
    } elseif ($row[8] === 'Sanguins') {
        $keputusan = "<font color=red>$row[8]</font>";
    } elseif ($row[8] === '?') {
        $keputusan = "<font color=black>$row[8]</font>";
    } else {
        $keputusan = "<b>$row[8]</b>";
    }
    echo "<font color=red>$row[1]</font> = $row[2] (Koleris = $row[4], Melankolis = $row[5], Plegmatis = $row[6], Sanguins = $row[7]) : <b>$keputusan</b><br>";

    /*panggil dirinya sendiri*/
    get_subfolder($row[0], $spasi + 1);

}