-1

I'm trying to check id database connection exist or not then redirect to a page where a user can be able to create a database through form inputs.

Here's what I tried to do but not succeed.

public function index()
{
    // $database = \Config\Database::connect();
    $database = \CodeIgniter\Database\Config::getConnections();

    if ($database) 
    {
        //db connection initialized
        return view('dashboard');
    }
    else
    {
        //db connection  not initialized
        return redirect('db_setup');
    }

}
Jonathan
  • 1,955
  • 5
  • 30
  • 50
ven
  • 185
  • 1
  • 18

2 Answers2

0

You have syntax error in redirect function and I am not sure about getconnections function what you have implemented there but connect () will work. See updated code

$database = \Config\Database::connect();
    if ($database) 
    {
        //db connection initialized
        return view('dashboard');
    }
    else
    {
        //db connection  not initialized
        return redirect()->to('db_setup');
    }
Virender Kumar
  • 182
  • 1
  • 8
  • Thank you. But the problem now is when Database name is wrong it still return true. How can I fix that? – ven Jul 14 '21 at 09:28
  • Thank Virender Kumar. This condition return true to both if & else. But my problem is to be able to bypass throwing error when db is incorrect to bd class/ .env file. But I did't find A method that check it and return true or false without throwing an exception error. – ven Jul 31 '21 at 09:41
0

Using the code from Virender Kumar but extending it with your request for dbname verification, use something like this:

$database = \Config\Database::connect();
if ($database) 
{
    // check if db exists:
    if($database->query("SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME='yourDbName'")) {
        //db connection initialized
        return view('dashboard');
    } else {
        return false; // db not exist
    }
} else {
    //db connection  not initialized
    return redirect()->to('db_setup');
}

Explanation: I added a CodeIgniter 4 raw query $db->query() that selects the general information schema from the database. If the database does not exists you'll get an error.

Jonathan
  • 1,955
  • 5
  • 30
  • 50
  • Thanks Jonathan.I changed $database to $db, But If I run $db->query(" THE SQL ABOVE") I get Error even if the correct dbname that exist passed. It return Error from SYSTEMPATH\Database\BaseConnection.php at line 398 with a message "Unable to connect to the database.". How can I fix this first? – ven Jul 30 '21 at 16:46
  • Try using `$database->()` instead of `$db->query()` – Jonathan Jul 30 '21 at 19:19
  • The problem is not changing $database->query(''), What I get then is error instead of being redirected if db not found. It force me to change db config option on Database class or .env which is not what I want. I tried a lots but since the mysqli/pdo throw an exception error when db not initialized/or incorrect db. – ven Jul 31 '21 at 09:26
  • What I get after running $database->query("SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME='ecommerceDb'"); Is checking the $this->connId(). can you please check what happen if you put dbname that not exists in .env i.e database.default.database = wrongDbName – ven Jul 31 '21 at 09:31
  • This statement works fine If I run on phpMyadmin. "SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME='ecommerceDb'". But the problem is when I try with $database->query("THAT QUERY"); Since $database check .env or Dababase Class configurations if they are wrong It throw an Exceptions error. – ven Jul 31 '21 at 09:36
  • That sounds like a **permissions problem**. Check your database config in `app\Config\database.php` and your `.env` for your production database' settings. You're probably using a different user for Codigniter and phpmyadmin. Or CI's mysql-authentication method is wrong. To troubleshoot such problems you can always enable error reporting and - logging and check those logfiles. – Jonathan Jul 31 '21 at 11:22
  • I have done some projects with ci4.0.4 now with ci4.1.13, so setting db configuration is not an issue here. The idea now is to create a method responsible to check if database exists and if not to be able be redirected to a page that have a html form input to fill db configs like host, user, pass and dbname and be able to change .env/ database config options with the help of php function preg_replace(). But the problem here is when no db is correct it throw an error instead of being redirected to the setup page. – ven Jul 31 '21 at 19:40
  • I know myqli or pdo class always throw error message when it did't find the correct dbname initialized. So Is there any way I can make this to work? – ven Jul 31 '21 at 19:41
  • Thank you very much Jonathan for your help. But can that be possible? – ven Jul 31 '21 at 19:46
  • I get your concept. Querying for a schema is the common way to check for a database existance. I don't see why this shouldnt work. It is not a conceptual problem but a very individual related to your (server/ci)-configuration or your specific code. For me, using a debugger like xdebug helped a lot to understand where and why I get certain results when using a MVC-Framework. – Jonathan Jul 31 '21 at 19:56
  • Thank you. I started to get an Idea about checking for Schema. I'll try with with this info https://www.php.net/manual/en/mysql-xdevapi-schema.existsindatabase.php – ven Jul 31 '21 at 21:30
  • How did you find this? Why no plain query? This function `existsInDatabase()` is not even documented. ?! It wont help you. I use php since 15 years and never even heard of xdevapi – Jonathan Aug 01 '21 at 09:12
  • Ok. Even me I see this force me to manupulate apache and changing some settings in php.ini to use mysql_xdeapi. So how can I achieve this with query? – ven Aug 01 '21 at 20:40
  • Hello, I need your help with this : https://stackoverflow.com/questions/68705159/unable-to-set-login-remember-me-cookies-when-redirect-initialized-ci4 – ven Aug 08 '21 at 22:26