1.下载 XAMPP 7.3.5 https://www.apachefriends.org/index.html
2.下载 Microsoft Drivers 5.6 for PHP for SQL Server https://www.microsoft.com/en-us/download/details.aspx?id=57916
3.下载 Microsoft ODBC Driver 17 for SQL Server https://www.microsoft.com/zh-CN/download/details.aspx?id=56567
所需软件如上,php.ini增加:
extension=php_sqlsrv_73_ts_x64.dll
启动apache后,在htdocs里编个文件sqlserver-test.php测试:
'sa','PWD' =>'pass','Database' =>'MyDB');
$conn = sqlsrv_connect($serverName,$connectionInfo);
if($conn){
echo "ok";
}else{
echo "err.";
die(print_r(sqlsrv_errors(),true));
}
?>
4.配置CI
1)database.php配置:
*/
$active_group = 'default';//'default';
$query_builder = TRUE;
$db['default'] = array(
'dsn' => '',
'hostname' => 'IP',
'port' => 'PORT',
'username' => 'sa',
'password' => 'pass',
'database' => 'MyDB',
'dbdriver' => 'sqlsrv',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);
2)在控制器Welcome.php增加一个函数代码,做一下测试:
function mssqltest(){
$this->load->database();
$query = $this->db->query("select * from products");
foreach ($query->result() as $row)
{
echo $row->pr_id;
echo $row->pr_name;
echo $row->pr_date;
}
echo 'Total Results: ' . $query->num_rows();
}
3)在浏览器浏览测试:
http://localhost/index.php/welcome/mssqltest