您好我使用Codeigniter和MSSQL我连接了数据库及其工作,我需要使用模型控制器中的连接字符串。以下是我在数据库.php文件中给出的以下代码
出于测试目的,我在这个文件中给出了一个查询并且它正在工作
$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;
}
这是模型文件。如何调用模型内的数据库连接 谢谢
答案 0 :(得分:1)
我正在查看它在加载数据库时所说的Codeigniter
文档,它会像......但是不要用力尝试!:
protected $MyMSSQL;
function __construct()
{
parent::__construct();
$this->MyMSSQL = $this->load->database('default',true);
}
function welcome_check()
{
$db = $this->MyMSSQL;
$stmt = $db->query("SELECT * from dbo.LAYER");
while($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
$result_array[] = $row;
}
print("<pre>");
print_r($result_array);
exit;
}
答案 1 :(得分:0)
经过大量的讨论后,我能够在我的代码(模型)中连接SQL Server,下面是我的数据库文件和模型中的示例函数:
我正在从我的模型中添加一个示例函数:
public function getCommandFields($name)
{
// Load Sql Server Connection
$this->sqlsrvr = $this->load->database('test', true);
$qry ="select SPECIFIC_NAME, ORDINAL_POSITION, PARAMETER_NAME, DATA_TYPE,
CHARACTER_MAXIMUM_LENGTH,PARAMETER_MODE
FROM INFORMATION_SCHEMA.Parameters
WHERE SPECIFIC_NAME ='".$name."'";
$query = $this->sqlsrvr->query($qry);
$fields = array();
foreach ($query->result_array() as $row)
{
$fields[]= $row;
}
return $fields;
}
数据库文件
$db['test']['hostname'] = "192.168.12.123";
$db['test']['username'] = "sa";
$db['test']['password'] = "password";
$db['test']['database'] = "Hospital";
$db['test']['dbdriver'] = "sqlsrv";
$db['test']['dbprefix'] = "";
$db['test']['pconnect'] = FALSE;
$db['test']['db_debug'] = FALSE;
$db['test']['cache_on'] = FALSE;
$db['test']['cachedir'] = "";
$db['test']['char_set'] = "utf8";
$db['test']['dbcollat'] = "utf8_general_ci";
$db['test']['swap_pre'] = "";
$db['test']['autoinit'] = TRUE;
$db['test']['stricton'] = FALSE;