您好我使用MSSQL的Codeigniter我已連接數據庫及其工作,我需要在模型控制器中使用連接字符串。這是下面的代碼,我在數據庫中給出的PHP文件如何在模型中執行sqlsrv_query?如何調用模型內的數據庫連接?
用於測試目的,我在這個文件中給出的查詢本身及其工作
$db['default']['hostname'] = "Admin\SQLEXPRESS"; // or put the IP of your SQL Server Instance
$db['default']['port'] = 1433; // or the port you configured on step 6
$db['default']['username'] = '';
$db['default']['password'] = '';
$db['default']['database'] = 'db_name';
$db['default']['dbdriver'] = 'sqlsrv';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = FALSE; // Pay attention to this, codeigniter makes true for default
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = '';
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_general_ci';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;
$serverName = $db['default']['hostname']; //serverName\instanceName
// Since UID and PWD are not specified in the $connectionInfo array,
// The connection will be attempted using Windows Authentication.
$connectionInfo = array('Database' => $db['default']['database']);
$conn = sqlsrv_connect($serverName, $connectionInfo);
if ($conn)
{
$sql="SELECT * from dbo.LAYER";
$stmt = sqlsrv_query($conn,$sql);
while($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC))
{
$result_array[] = $row;
}
print("<pre>");
print_r($result_array);
exit;
}
else
{
echo "Connection could not be established.<br />";
die (print_r(sqlsrv_errors(), true));
}
和模式,我試過,但它不工作
class welcome_m extends CI_Model{
function __construct()
{
parent::__construct();
$this->load->database('default',true);
}
function welcome_check()
{
$sql="SELECT * from dbo.LAYER";
$stmt = $this->db->sqlsrv_query($sql);
while($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC))
{
$result_array[] = $row;
}
print("<pre>");
print_r($result_array);
exit;
}
這是模型文件。如何調用內部模型 謝謝
添加它到'welcome_check($康恩)'。 – Rasclatt 2014-11-06 05:44:13
如何添加此功能是從控制器調用知道 – Athi 2014-11-06 05:49:10
我只是拋出的想法,說實話,我不知道。爲什麼你不能在這個類中創建連接,因爲你正在擴展它呢?如果你在你的其他部分工作,爲什麼你不能把這些代碼放到'welcome_check'中? – Rasclatt 2014-11-06 05:53:40