2016-03-28 93 views
-6

我想笨3與Microsoft SQL Server 2008連接笨3的MS SQL Server

連接,但我收到提示服務器錯誤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 
); 

我試圖用MS SQL連接簡單的PHP核心文件的代碼,它是連接成功。

以下是核心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 server 2008與Xampp,Windows 7 32位。

+0

「* ..我想PHP腳本MS SQL Server 2008中*連接」。那麼,你的代碼在哪裏?我們不能假設你的代碼。我們不會坐下來觀看那個視頻。而且,「* ..我仍然有同樣的錯誤。*」。什麼錯誤? –

+0

是的,這是可能的,相信我! – rray

+0

你有沒有考慮過先學習php? – SnakeFoot

回答

1

如果使用CodeIginiter成功連接到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