如何在代码点火器格式中使用以下简单查询?

时间:2013-06-03 11:50:17

标签: mysql sql codeigniter

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

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

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

count  student_name  student_info
1        Ram          Palpa
2        Shyam        Butwal

1 个答案:

答案 0 :(得分:0)

使用 CodeIgniter's Database Custom Function Calls

假设您在 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";
}