1
SET @rownum:=0;
SELECT @rownum:=@rownum+1 as count, student_name,student_info FROM studnet;

我想将此查询合并到代码点火器模型中...

我希望输出如下,其中计数是动态的,即随着记录的增加而增加:::

count  student_name  student_info
1        Ram          Palpa
2        Shyam        Butwal
4

1 回答 1

0

使用 CodeIgniter 的数据库自定义函数调用

假设您在application/config/database.php中设置了mysqli

$db['default']['dbdriver'] = 'mysqli';

然后在你的模型中:

$this->load->database();

// Perform a mysqli_multi_query
$db_id = $this->db->conn_id;
$this->db->call_function("multi_query", $db_id, "SET @rownum:=0; SELECT @rownum:=@rownum+1 as count, student_name,student_info FROM student;"
$this->db->call_function('next_result', $db_id);  // Skip the first query in this multi_query since you want the result from the second query
$result = $this->db->call_function("store_result", $db_id);

// Output each row
while($row = $result->fetch_assoc()){
    $row['count'] . " ". $row['student_name'] . " " . $row['student_info'] . "\n";
}
于 2013-06-05T02:47:44.790 回答