【问题标题】:How to use following simple query in code igniter format?如何以代码点火器格式使用以下简单查询?
【发布时间】:2013-06-03 11:50:16
【问题描述】:
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

【问题讨论】:

  • 尝试将变量初始化部分添加到 join ex SELECT @rownum:=@rownum+1 as count, student_name,student_info FROM studnet JOIN (SELECT @rownum:=0)tmp;

标签: mysql sql codeigniter


【解决方案1】:

使用 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";
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-03
    • 2016-11-13
    • 2014-12-23
    • 2016-05-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多