codeigniter mysql查询不起作用

I know this isn't good practice to have the queries within a controller, but for some reason they do not work within this controller class. I'm not too experienced with codeigniter, so any help is appreciated, as I have no idea on how else to set this up to work properly. I have an ajax script that gets the information.

Here is my code:

<?php

class Phonecall extends CI_Controller {

    public function index() {

        /*$con = mysqli_connect('localhost','root','root','MYDB');
        if (!$con) {
            die ('Could not connect: ' . mysqli_error($con));
        }*/
        //$operatorId = $_SESSION['Oper']['OperatorID']; //I also cannot figure out how to get $_SESSION information either
        //$sql = "SELECT phone_number FROM incoming_calls WHERE OperatorID='${operatorId}'";
        //$result = mysqli_query($con,$sql);
        $query = $this->db->query("SELECT phone_number FROM MYDB.incoming_calls");// WHERE OperatorID='${operatorId}'");

        while ($row = $query->row_array()) {
            $number = $row['phone_number'];
        }

        /*$sql = "SELECT Username, UserID, Name
            FROM tblUsers 
            WHERE PhoneHome='999-999-9999' OR PhoneCell='999-999-9999' OR PhoneWork='999-999-9999'";
        */

        $query = $this->db->query("SELECT Username, UserID, Name
            FROM MYDB.tblUsers 
            WHERE PhoneHome='999-999-9999' OR PhoneCell='999-999-9999' OR PhoneWork='999-999-9999'");

        while ($row = $query->row_array()) {
            $userArray[] = array("name" => $row['Name'], "username" => $row['Username'], "user_id" => $row['UserID']);
        }

        if (!empty($userArray)) {
            echo json_encode($userArray);
        }

        if (isset($_POST["drop"])) {
            $query = $this->db->query("DELETE FROM MYDB.incoming_calls 
                WHERE phone_number = $number
                LIMIT 1");
            if (!$result) {
                die ('Could not drop row: ');
            }
        }

        $this->db->close();
    }
}

?>

Here is my JS:

    var user = new Array();
    var user_id = new Array();
    var name = new Array();

    $.get(baseURL + 'phonecall/index', function(data) {//where baseURL is defined
        var loginInfo = jQuery.parseJSON(data);

        for (var i = 0; i < loginInfo.length; ++i) {
            name[i] = loginInfo[i].name;
            user[i] = loginInfo[i].username;
            user_id[i] = loginInfo[i].user_id;
        }
    }

I do not know much about frameworks and I'm trying to learn, I also do not know how to make this file access the $_SESSION data. Any help is greatly appreciated.

douchan4674
douchan4674 在服务器日志中查找500错误的原因。
接近 6 年之前 回复
dongmaxi6763
dongmaxi6763 使用codeigniter提供给您的MVC模式。放置模型(用于您的SQL查询)和会话(保存并正确访问它们)库。然后你就会知道错误来自哪里。
接近 6 年之前 回复
douao1579
douao1579 你在database.php文件中将引擎更改为mysql(如果你没有使用mysqli)?
接近 6 年之前 回复
duan7664
duan7664 当ajax脚本尝试运行时,我得到的错误是控制台中的服务器500错误。如果我在我的本地机器上运行它,并为mysqli查询更改代码一切都很完美。除了500服务器错误之外,它不会给我任何其他信息。
接近 6 年之前 回复
dsgdf45654
dsgdf45654 什么不能正常工作?你收到错误了吗?至于会话数据,我建议您阅读手册:ellislab.com/codeigniter/user-guide/libraries/sessions.html
接近 6 年之前 回复
duankange2433
duankange2433 这些查询在顺便运行时会返回数据。
接近 6 年之前 回复

1个回答

Always use model for DB queries.

For first query use the following code:

$this->db->select("phone_number");
$this->db->from("incoming_calls");
$query = $this->db->get();
return $query->result_array();

For the secound query use this:

$this->db->select("Username, UserID, Name");
$this->db->from("tblUsers");
$this->db->where("PhoneHome,'999-999-9999'");
$this->db->where("PhoneCell,'999-999-9999'");
$this->db->where("PhoneWork,'999-999-9999'");
$query = $this->db->get();
return $query->result_array();

Be sure to make 2 different functions for the queries in model.

Then at your controller side use the following code:

$data['first_record'] = $this->your_modal_name->your_first_function_name();
$data['secound_record'] = $this->your_modal_name->your_second_function_name();

Then run this data in foreach, and it will give you the record, which you want.

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问