dongsaolian8786 2016-03-28 13:13
浏览 67
已采纳

将Codeigniter 3与MS SQL Server连接

I'm trying to connect Codeigniter 3 with Microsoft SQL Server 2008

But I'm getting error Server error 500.

I'm attaching code of 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
 );

I tried to connect simple core php file with MS SQL and it is connect successfully.

Following is the core PHP file code.

<?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.");
}
?>

I'm using PHP 5.6 and Sql server 2008 with Xampp, Windows 7 32 bit.

  • 写回答

1条回答 默认 最新

  • duanmu0834 2016-03-31 15:04
    关注

    If you have successfully connected to the MS SQL server using CodeIginiter you do not need the second connection in part 2 of your code. Just call $this->db->query("INSERT INTO ....") That is all. CI does the rest for you. (remove odbc_connect and odbc_exec) and use the built in CI database class.

    Your Code would look like this

    $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);
    

    Reference:

    https://ellislab.com/codeIgniter/user-guide/database/examples.html

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?