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.

    评论

报告相同问题?

悬赏问题

  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题