douji8017 2014-12-23 07:23
浏览 63

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.

  • 写回答

1条回答

  • dream_life5200 2014-12-25 09:37
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥20 Python安装cvxpy库出问题
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥15 python天天向上类似问题,但没有清零
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 C#调用python代码(python带有库)
  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题