如何在 CodeIgniter 中手动连接到数据库?

发布于 2024-09-25 13:40:37 字数 271 浏览 0 评论 0原文

是否可以在 CodeIgniter 中使用任何 php 驱动程序手动连接到数据库?我会在模型中打开一个连接吗?我有一个 CodeIgniter 无法满足的需求,但我想将它用于它的 MVC 架构。我只是不能像在 MySQL 等演示中那样使用它的 ActiveRecord。

如果我只能进行“常规”非 CodeIgniter 数据库连接,我如何将信息获取到我的控制器中?

另外,我想以这种方式使用 CodeIgniter (没有活动记录)但对于它的所有其他“东西”是错误的吗?

谢谢。

Is it possible to connect to a database manually, using any set of drivers for php, in CodeIgniter? Would I just open a connection in the model? I have a need that CodeIgniter won't cover but I'd like to use it for it's MVC architecture. I just can't use it's ActiveRecord like in the demos with MySQL, etc.

If I can just do a "regular" non-CodeIgniter database connection, how do I get the information into my controller?

Also, am I wrong for wanting to use CodeIgniter in this way (no active record) but for all its other "stuff"?

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

奈何桥上唱咆哮 2024-10-02 13:40:37

这是一个使用函数式 php 连接到 mysql 数据库的非常简单的示例。它来自 php 手册。将其放入您的模型中。通过正常 CI 方式调用模型函数来调用它。它将返回结果数组,因此将模型调用分配给控制器中的变量。

<?php
function my_model_query(){
    //replace the function arguments with your information
    $link = mysql_connect('host_name', 'mysql_user', 'mysql_password');
    if (!$link) {
        die('Could not connect: ' . mysql_error());
    }
    //your code here
    $query = "SELECT * FROM mytable";
    $result = mysql_query($query);
    while($row = mysql_fetch_assoc($result)){

        $result_array[]['your_field_1'] = $row['your_field_1'];
        $result_array[]['your_field_2'] = $row['your_field_2']; 
        //and so on

       }

    //close the connection when you are done
    mysql_close($link);

    //send results back to controller
    return $result_array;


}//endfunction
?>

您可以在模型方法中使用它。假设您返回结果

编辑:将 mysql_close 移至 return 语句上方

This is a very simple example of using functional php to connect to a mysql db. It comes form the php manual. Put this in your model. Call it by calling a model function the normal CI way. It will return the result array, so assign the model call to a variable in your controller.

<?php
function my_model_query(){
    //replace the function arguments with your information
    $link = mysql_connect('host_name', 'mysql_user', 'mysql_password');
    if (!$link) {
        die('Could not connect: ' . mysql_error());
    }
    //your code here
    $query = "SELECT * FROM mytable";
    $result = mysql_query($query);
    while($row = mysql_fetch_assoc($result)){

        $result_array[]['your_field_1'] = $row['your_field_1'];
        $result_array[]['your_field_2'] = $row['your_field_2']; 
        //and so on

       }

    //close the connection when you are done
    mysql_close($link);

    //send results back to controller
    return $result_array;


}//endfunction
?>

You can use this in your model method. Let's assume you return the results

EDIT: moved mysql_close above return statement

[旋木] 2024-10-02 13:40:37

我真的不知道你的问题是什么,但你可以在没有活动记录的情况下执行查询。

例如

$this->db->query();

I don't really know what your problem is, but you can execute a query without the active record.

e.g

$this->db->query();
束缚m 2024-10-02 13:40:37

对于手动连接数据库,只需普通代码

$link = mysqli_connect('localhost','root','','db_iris');

for manual connection to database , just the normal code

$link = mysqli_connect('localhost','root','','db_iris');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文