0
我知道在控制器中進行查詢並不是一個好習慣,但由於某些原因,它們在此控制器類中不起作用。我對codeigniter不太瞭解,因此我非常欣賞任何幫助,因爲我不知道如何設置它以正常工作。我有一個獲取信息的ajax腳本。codeigniter mysql查詢不起作用
這裏是我的代碼:
<?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數據。任何幫助是極大的讚賞。
這些查詢確實會在數據運行時返回數據。 – Juan
什麼不正確?你有錯誤嗎? 至於會話數據,我建議您閱讀手冊: https://ellislab.com/codeigniter/user-guide/libraries/sessions.html – Patrick
我得到的錯誤是在控制檯中的服務器500錯誤當ajax腳本嘗試運行時。如果我在我的本地機器上運行它,並且將代碼稍微更改爲mysqli查詢,那麼一切正常。除了500服務器錯誤之外,它不會給我任何其他信息。 – Juan