如何从CodeIgniter 3连接到SQL Server数据库?

时间:2019-01-21 04:14:25

标签: php sql-server codeigniter

我正在尝试将CodeIgniter 3项目连接到运行WAMP的Web服务器上的SQL Server 2008。

我尝试使用sqlsrv,odbc和pdo dbdrivers连接到数据库,但仍然无法执行。

这是我到目前为止尝试过的,

  1. 已安装PHP的sqlsrv驱动程序并已安装。
  2. sqlsrvpdo_sqlsrv出现在WAMP本地主机页面和phpinfo()页面上

enter image description here

这是我的database.php文件,

$active_group = 'default';
$query_builder = TRUE;

$db['default'] = array(
    'dsn'   => '',
    'hostname' => '172.20.15.52',
    'username' => 'ivrserver',
    'password' => 'ivrserver',
    'database' => 'HMS-Migration',
    'dbdriver' => 'sqlsrv',
    'dbprefix' => '',
    'pconnect' => FALSE,
    'db_debug' => (ENVIRONMENT !== 'production'),
    'cache_on' => FALSE,
    'cachedir' => '',
    'char_set' => 'utf8',
    'dbcollat' => 'utf8_general_ci',
    'swap_pre' => '',
    'encrypt' => FALSE,
    'compress' => FALSE,
    'stricton' => FALSE,
    'failover' => array(),
    'save_queries' => TRUE

);

但是我在项目的每个页面上都遇到此错误,

A Database Error Occurred

Unable to connect to your database server using the provided settings.
Filename: C:/wamp64/www/ghms/system/database/DB_driver.php
Line Number: 436

我尝试使用ODBC连接器。我从ODBC数据源创建了系统DNS,该数据源连接到SQL Server数据库并以以下方式更改database.php,

$db['default'] = array(
    'dsn'   => '',
    'hostname' => 'mssqltest',
    'username' => 'ivrserver',
    'password' => 'ivrserver',
    'database' => 'HMS-Migration',
    'dbdriver' => 'odbc',
    'dbprefix' => '',
    'pconnect' => FALSE,
    'db_debug' => (ENVIRONMENT !== 'production'),
    'cache_on' => FALSE,
    'cachedir' => '',
    'char_set' => 'utf8',
    'dbcollat' => 'utf8_general_ci',
    'swap_pre' => '',
    'encrypt' => FALSE,
    'compress' => FALSE,
    'stricton' => FALSE,
    'failover' => array(),
    'save_queries' => TRUE
);

这能够连接数据库,但结果为Query Builder doesn't work with ODBC。 ¯\ _(ツ)_ /¯

然后我尝试使用pdo连接到SQL Server,并对database.php进行了以下更改

$db['default'] = array(
    'dsn'   => 'sqlsrv:server=172.20.15.52,1433;Database=HMS-Migration',
    'hostname' => '',
    'username' => 'ivrserver',
    'password' => 'ivrserver',
    'database' => '',
    'dbdriver' => 'pdo',
    'dbprefix' => '',
    'pconnect' => FALSE,
    'db_debug' => (ENVIRONMENT !== 'production'),
    'cache_on' => FALSE,
    'cachedir' => '',
    'char_set' => 'utf8',
    'dbcollat' => 'utf8_general_ci',
    'swap_pre' => '',
    'encrypt' => FALSE,
    'compress' => FALSE,
    'stricton' => FALSE,
    'failover' => array(),
    'save_queries' => TRUE
);

这要求我为PHP安装sql驱动程序,但我想我已经安装了它们!

A Database Error Occurred

SQLSTATE[IMSSP]: This extension requires the Microsoft ODBC Driver for SQL Server to communicate with SQL Server. Access the following URL to download the ODBC Driver for SQL Server for x64: https://go.microsoft.com/fwlink/?LinkId=163712

Filename: C:/wamp64/www/ghms/system/database/drivers/pdo/pdo_driver.php
Line Number: 142

我回去检查驱动程序的安装,

  1. 我有启用了线程安全性的php 7.0.32 x64
  2. 检查C:\ wamp64 \ bin \ php \ php7.0.32 \ ext是否正确.dll
php_pdo_sqlsrv_7_ts_x64.dll
php_sqlsrv_7_ts_x64.dll
  1. 已检查php.ini文件
extension=php_pdo_sqlsrv_7_ts_x64.dll
extension=php_sqlsrv_7_ts_x64.dll
  1. 两个扩展都作为启用的PHP扩展显示在wamp的托盘菜单中

问题

尽管我已经尽力检查了所有事情,因为它仍然没有连接到数据库-我在某个地方一定是错的。将CodeIgniter 3项目连接到SQL Server的正确方法是什么?

编辑

我想我找到了解决方案。 pdo dbdriver上的错误要求我安装用于MS SQL的ODBC驱动程序,但我却检查了用于MS SQL的PHP​​驱动程序。我去了错误中的链接,下载了ODBC驱动程序,现在看来可以正常工作!愚蠢的我!

1 个答案:

答案 0 :(得分:0)

尝试此操作。定义您的sqlsrv:

AlertDialog.Builder builder = new AlertDialog.Builder(context);//Context is activity context
final EditText input = new EditText(context);
builder.setTitle(getString(R.string.remove_item_dialog_title));
        builder.setMessage(getString(R.string.dialog_message_remove_item));
 builder.setTitle(getString(R.string.update_qty));
            builder.setMessage("");
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.MATCH_PARENT);
            input.setLayoutParams(lp);
            input.setHint(getString(R.string.enter_qty));
            input.setTextColor(ContextCompat.getColor(context, R.color.textColor));
            input.setInputType(InputType.TYPE_CLASS_NUMBER);
            input.setText("String in edit text you want");
            builder.setView(input);
   builder.setPositiveButton(getString(android.R.string.ok),
                (dialog, which) -> {

//Positive button click event
  });

 builder.setNegativeButton(getString(android.R.string.cancel),
                (dialog, which) -> {
//Negative button click event
                });
        AlertDialog dialog = builder.create();
        dialog.show();