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 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置