codeigniter mysql查询不起作用

时间:2014-12-23 07:23:03

标签: php mysql codeigniter

我知道在控制器中进行查询并不是一种好的做法,但由于某种原因,它们无法在此控制器类中工作。我对codeigniter不太熟悉,所以任何帮助都会受到赞赏,因为我不知道如何设置它以使其正常工作。我有一个获取信息的ajax脚本。

这是我的代码:

<?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();
    }
}

?>

这是我的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;
        }
    }

我对框架知之甚少,而且我正在努力学习,我也不知道如何让这个文件访问$ _SESSION数据。非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

始终使用模型进行数据库查询。

对于第一个查询,请使用以下代码:

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

对于secound查询,请使用:

$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();

确保为模型中的查询创建2个不同的函数。

然后在您的控制器端使用以下代码:

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

然后在foreach中运行此数据,它会为您提供所需的记录。