Welcome Guest, Not a member yet? Register   Sign In
Setup Database and check connection
#1

I have my first attempt(i never used a framework) to use CodeIgniter 4.

What I try to achieve is to setup database in CodeIgniter and check in controller if it works.

For database I use MySQL Community Server 8.0.19: https://dev.mysql.com/downloads/mysql/

This is the controller:

Code:
class Testdb extends BaseController{
    
    public function index(){
    
        $db = \Config\Database::connect();
            
    $sel = $db->query("SELECT 1 AS one");
            
    var_dump( mysqli_fetch_assoc($sel)["one"] ); //mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, object given
            
    return view('testdb'); //empty view
        
    }

}

And this is Database.php:

Code:
public $default = [
    'DSN'      => '',
    'hostname' => 'localhost',
    'username' => 'root',
    'password' => 'myPass',
    'database' => 'mydb',
    'DBDriver' => 'MySQLi',
    'DBPrefix' => '',
    'pConnect' => false,
    'DBDebug'  => (ENVIRONMENT !== 'production'),
    'cacheOn'  => false,
    'cacheDir' => '',
    'charset'  => 'utf8mb4',
    'DBCollat' => 'utf8mb4_unicode_ci',
    'swapPre'  => '',
    'encrypt'  => false,
    'compress' => false,
    'strictOn' => false,
    'failover' => [],
    'port'     => 3306,
];

When I check in browser I get this error: `mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, object given`.
I test the connection for db with MySQL Workbench and everything works fine.

What I do wrong and how to fix it?
Reply
#2

Hi the problem here are that you are using CodeIgniters built in Query Builder, and then trying to access the data with mysqli built in functions. You need to stick to CodeIgniters own function or switch over completely to mysqli_ functions.

Here are how you are supposed to fecth data:
https://codeigniter4.github.io/userguide...sults.html

PS. Moved your thread into CI 4 Support.
Reply
#3

I changed `var_dump(...)` with `var_dump($sel->getRow(0)->one);` and now it works.
Thank you.
I thought I can combine CI4 with mysqli
Reply




Theme © iAndrew 2016 - Forum software by © MyBB