簡體   English   中英

將 Codeigniter 3 與 MS SQL Server 連接

[英]Connect Codeigniter 3 with MS SQL Server

我正在嘗試將 Codeigniter 3 與 Microsoft SQL Server 2008 連接

但我收到錯誤Server error 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.");
}
?>

我在 Xampp、Windows 7 32 位上使用 PHP 5.6 和 Sql server 2008。

如果您已使用 CodeIginter 成功連接到 MS SQL 服務器,則不需要代碼第 2 部分中的第二個連接。 只需調用$this->db->query("INSERT INTO ....")這就是全部。 CI 為您完成剩下的工作。 (刪除odbc_connectodbc_exec )並使用內置的 CI 數據庫類。

你的代碼看起來像這樣

$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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM