我正在尝试从外部sql数据库中获取员工列表我已经知道使用常规php页面获取此信息,但我是codeigniter的新手,我想试试这是否可行。
所以我的controller.php看起来像这样。
class Patients extends CI_Controller {
function index() {
$data['title'] = 'Patients';
$data['patients'] = $this->Patients_model->get_patients();
$this->load->view('templates/header');
$this->load->view('patients/index', $data);
$this->load->view('templates/footer');
print_r($data);
}
我把print_r($data)
看看它输出是否正确,我确实得到了下面的数组数据,但它很奇怪,因为它只返回一个数据,因为我的sqlsrv_query声明前30位,无论如何。
Array ( [title] => Patients [patients] => Array ( [employee_i] => 3321 [first_name] => Zyriel [last_name] => Tanawan [labor_clas] => SD [current_de] => WORKSHOP - L.A [date_emplo] => 2016-04-06 00:00:00 [pay_code] => 06 [cost_cente] => 7723 [file_statu] => EMPLOYEE [middle_nam] => Martinez [vacation_l] => 11.0000 [sick_leave] => 0.0000 [line_code] => Facilities Workshop - Laguna [gender] => M [approver_1] => 950812 [approver_2] => 220364 [approver_3] => 940016 [leave_as_o] => 2017-05-19 00:00:00 [section_ma] => [rest_day1] => Sun [rest_day2] => [Current_po] => Machinist [Station] => Laguna ) )
这是我的model.php看起来像。
class Patients_model extends CI_Model
{
public function get_patients(){
$serverName = "MYSERVERNAME"; //serverName\instanceName
$connectionInfo = array( "Database"=>"MYDATABASE", "UID"=>"USERID", "PWD"=>'MYPASSWORD');
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn === false ) {
die( print_r( sqlsrv_errors(), true));
}
$sql = "SELECT top 30 * from empmas ORDER BY first_name DESC";
$stmt = sqlsrv_query( $conn, $sql );
if( $stmt === false) {
die( print_r( sqlsrv_errors(), true) );
}
return sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC);
}
}
现在我的view.php
<?php foreach ($patients as $rows) :?>
<p><?php echo $rows['employee_i'];?></p><br/>
<?php endforeach;?>
但我得到以下错误
遇到PHP错误
严重性:警告
消息:非法字符串偏移&#39; employee_i&#39;
我已经尝试过其他类似问题的一些答案,但是当您使用sqlsrv_query
任何建议都有帮助时它似乎有所不同!谢谢!
答案 0 :(得分:0)
您的代码中存在错误,请在下方查看。您必须使用$rows
代替$value
<p><?php echo $rows['employee_i'];?></p><br/>