将 Codeigniter 3 与 MS SQL 服务器连接
Connect Codeigniter 3 with MS SQL Server
我正在尝试将 Codeigniter 3 与 Microsoft SQL Server 2008
连接
但我收到错误 服务器错误 500。
我附上 database.php
的代码
$db['default'] = array(
'dsn' => 'Driver={SQL Server Native Client 10.0};Server=(local);Database=my_db;',
'hostname' => '(local)',
'port' => '',
'username' => '',
'password' => '',
'database' => '',
'dbdriver' => 'odbc', // or mssql or sqlsrv
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => TRUE,
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
//'autoinit' => TRUE,
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);
我尝试将简单的核心 php 文件与 MS SQL 连接,并且连接成功。
以下是核心PHP文件代码。
<?php
// Replace the value of these variables with your own data
$user = '';
$pass = '';
$server = "(local)";
$database = 'h2g2';
// No changes needed from now on
$connection_string = "DRIVER={SQL Server};SERVER=$server;DATABASE=$database";
$conn = odbc_connect($connection_string,$user,$pass);
if ($conn) {
echo "Connection established.";
$sql = "INSERT INTO hg_users (u_uuid,u_name, u_email,u_new_email,u_password,u_display_name,u_forgot_token,u_forgot_token_request_time,u_verify_token,u_verified,u_last_login_date,u_last_login_ip,u_created_date,u_modified_date,u_status)
VALUES ('1231233', '123123123 sdfsdfdsdf','ASsASas','asdasd','asd','ewrt','fgh','sdfgsasd','asdasd','2','zx','ZXcZX','aSzxCASD','ASDzxzx','1');";
$result = odbc_exec($conn,$sql);
echo "<pre>";print_r($result);
die;
} else{
die("Connection could not be established.");
}
?>
我正在使用 PHP 5.6 和 Sql 服务器 2008,Xampp,Windows 7 32 位。
如果您已使用 CodeIginiter 成功连接到 MS SQL 服务器,则不需要代码第 2 部分中的第二次连接。只需调用 $this->db->query("INSERT INTO ....")
即可。 CI 为您完成剩下的工作。 (删除 odbc_connect
和 odbc_exec
)并使用内置的 CI 数据库 class.
您的代码如下所示
$sql = "INSERT INTO hg_users (u_uuid,u_name, u_email,u_new_email,u_password,u_display_name,u_forgot_token,u_forgot_token_request_time,u_verify_token,u_verified,u_last_login_date,u_last_login_ip,u_created_date,u_modified_date,u_status)
VALUES ('1231233', '123123123 sdfsdfdsdf','ASsASas','asdasd','asd','ewrt','fgh','sdfgsasd','asdasd','2','zx','ZXcZX','aSzxCASD','ASDzxzx','1');";
// runs the query to your MS SQL connection (automatically)
$this->db->query($sql);
参考:
https://ellislab.com/codeIgniter/user-guide/database/examples.html
我正在尝试将 Codeigniter 3 与 Microsoft SQL Server 2008
连接但我收到错误 服务器错误 500。
我附上 database.php
的代码$db['default'] = array(
'dsn' => 'Driver={SQL Server Native Client 10.0};Server=(local);Database=my_db;',
'hostname' => '(local)',
'port' => '',
'username' => '',
'password' => '',
'database' => '',
'dbdriver' => 'odbc', // or mssql or sqlsrv
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => TRUE,
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
//'autoinit' => TRUE,
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);
我尝试将简单的核心 php 文件与 MS SQL 连接,并且连接成功。
以下是核心PHP文件代码。
<?php
// Replace the value of these variables with your own data
$user = '';
$pass = '';
$server = "(local)";
$database = 'h2g2';
// No changes needed from now on
$connection_string = "DRIVER={SQL Server};SERVER=$server;DATABASE=$database";
$conn = odbc_connect($connection_string,$user,$pass);
if ($conn) {
echo "Connection established.";
$sql = "INSERT INTO hg_users (u_uuid,u_name, u_email,u_new_email,u_password,u_display_name,u_forgot_token,u_forgot_token_request_time,u_verify_token,u_verified,u_last_login_date,u_last_login_ip,u_created_date,u_modified_date,u_status)
VALUES ('1231233', '123123123 sdfsdfdsdf','ASsASas','asdasd','asd','ewrt','fgh','sdfgsasd','asdasd','2','zx','ZXcZX','aSzxCASD','ASDzxzx','1');";
$result = odbc_exec($conn,$sql);
echo "<pre>";print_r($result);
die;
} else{
die("Connection could not be established.");
}
?>
我正在使用 PHP 5.6 和 Sql 服务器 2008,Xampp,Windows 7 32 位。
如果您已使用 CodeIginiter 成功连接到 MS SQL 服务器,则不需要代码第 2 部分中的第二次连接。只需调用 $this->db->query("INSERT INTO ....")
即可。 CI 为您完成剩下的工作。 (删除 odbc_connect
和 odbc_exec
)并使用内置的 CI 数据库 class.
您的代码如下所示
$sql = "INSERT INTO hg_users (u_uuid,u_name, u_email,u_new_email,u_password,u_display_name,u_forgot_token,u_forgot_token_request_time,u_verify_token,u_verified,u_last_login_date,u_last_login_ip,u_created_date,u_modified_date,u_status)
VALUES ('1231233', '123123123 sdfsdfdsdf','ASsASas','asdasd','asd','ewrt','fgh','sdfgsasd','asdasd','2','zx','ZXcZX','aSzxCASD','ASDzxzx','1');";
// runs the query to your MS SQL connection (automatically)
$this->db->query($sql);
参考:
https://ellislab.com/codeIgniter/user-guide/database/examples.html