LoginSignup
4

More than 5 years have passed since last update.

【FuelPHP】mysql接続でこけた話

Last updated at Posted at 2016-12-14

- 開発環境

OS: X EI Capitan 10.11.6
PHP: 5.5.36
FuelPHP: 1.7.3
MySQL: 5.7.16

結論としてはFuelPHPでMySQLを使うための設定に抜けがあったことが原因.
今回は、拡張モジュール mysqli を使用した.

- 作業内容

localhostで開発をしていてDB接続するために,
fuel/app/config/development/db.php を下記でセット.

return array(
   'default' => array(
      'connection'  => array(
         'hostname' => 'localhost',
         'port'     => '3306',
         'database' => 'test_db',
         'username' => user',
         'password' => ‘xxxx',
      ),
        'profiling' => true,
   ),
);

次にfuel/app/config/db.php を下記でセット.

return array(
    'default' => array(
        'type'        => 'mysqli',
        'connection'  => array(
            'persistent' => false,
        ),
        'identifier'   => '`',
        'table_prefix' => '',
        'charset'      => 'utf8',
        'collation'    => false,
        'enable_cache' => true,
        'profiling'    => false,
        'readonly'     => false,
    ),

);

DB設定はこれだけであとは DB保存するための Model を作って、
Controller でデータを save( ) してあげればイケると思ったけどエラーでた.

Fuel\Core\PhpErrorException: mysqli::mysqli(): (HY000/2002): No such file or directory

この設定が必要だった.
fuel/core/classes/database/mysqli/connection.php

// Extract the connection parameters, adding required variables
extract($this->_config['connection'] + array(
   'database'   => 'test_db',
   'hostname'   => 'localhost',
   'port'       => '3306',
   'socket'     => '/tmp/mysql.sock',
   'username'   => user',
   'password'   => ‘xxxx',
   'persistent' => false,
   'compress'  => true,
));

これで無事解決!

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4