Summer Sale! All accounts are 50% off this week.

zakariaarrid's avatar

Using new mysqli in laravel

Hello i have question please ! can i use mysqli in laravel application? it's possible thanks in advance

0 likes
8 replies
ahmeddabak's avatar

Yes it is possible, you can use it in your controller like you would use any other class, though i would suggest that you stick with laravel elequent, it is base on PDO, and many exploits are protected out of the box

ahmeddabak's avatar

It would be better to extract this to its own class

$db_config = Config::get('database.connections.'.Config::get('database.default'));

$mysqli = new mysqli($db_config["host"], $db_config["username"], $db_config["password"], $db_config["database"]);

if ($mysqli->connect_errno) {
    echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
Tray2's avatar

You can use anything that php allows in Laravel like mysqli but the question is why would you like to use it. PDO is much safer and unless you do some fancy SQL then I suggest you stick to Eloquent or the query builder.

kh_dev1's avatar

Before changing databases, you need to specify all databases in config/database.php. After that, you can change database connections on runtime (on the fly) but be careful. For example: //Default connection: DB::connection('table')->table('users')->all();

kh_dev1's avatar

in config/database.php 'mysql' => [ 'driver' => 'mysql', 'url' => env('DATABASE_URL'), 'host' => env('DB_HOST', '127.0.0.1'), 'port' => env('DB_PORT', '3306'), 'database' => env('DB_DATABASE','forge'), 'username' => env('DB_USERNAME', 'forge'), 'password' => env('DB_PASSWORD', ''), 'unix_socket' => env('DB_SOCKET', ''), 'charset' => 'utf8mb4', 'collation' => 'utf8mb4_unicode_ci', 'prefix' => '', 'prefix_indexes' => true, 'strict' => true, 'engine' => null, 'options' => extension_loaded('pdo_mysql') ? array_filter([ PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'), ]) : [], ],

    'mysql2' => [
        'driver' => 'mysql',
        'url' => env('DATABASE_URL'),
        'host' => env('DB_HOST', '127.0.0.1'),
        'port' => env('DB_PORT', '3306'),
        'database' => env('DB_DATABASE2','forge'),
        'username' => env('DB_USERNAME', 'root'),
        'password' => env('DB_PASSWORD', ''),
        'unix_socket' => env('DB_SOCKET', ''),
        'charset' => 'utf8mb4',
        'collation' => 'utf8mb4_unicode_ci',
        'prefix' => '',
        'prefix_indexes' => true,
        'strict' => true,
        'engine' => null,
        'options' => extension_loaded('pdo_mysql') ? array_filter([
            PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'), 
        ]) : [],
    ],

    'mysql3' => [
        'driver' => 'mysql',
        'url' => env('DATABASE_URL'),
        'host' => env('DB_HOST', '127.0.0.1'),
        'port' => env('DB_PORT', '3306'),
        'database' => env('DB_DATABASE3','db3'),
        'username' => env('DB_USERNAME', 'root'),
        'password' => env('DB_PASSWORD', ''),
        'unix_socket' => env('DB_SOCKET', ''),
        'charset' => 'utf8mb4',
        'collation' => 'utf8mb4_unicode_ci',
        'prefix' => '',
        'prefix_indexes' => true,
        'strict' => true,
        'engine' => null,
        'options' => extension_loaded('pdo_mysql') ? array_filter([
            PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
        ]) : [],
    ],

in .env file

DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=db1 DB_USERNAME=root DB_PASSWORD=

DB_DATABASE3=db3 DB_DATABASE2=db2

that db1 is a default database you can set new connection in the controllers like below

    $user = new Users;
    $user->setConnection('mysql2');
    dd($user->find(1));

Please or to participate in this conversation.