Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in SQL by (4k points)

I'm having a problem with a function that connects me to the database using mysqli. What I'm aiming to have is to just type getConnected(); where I need the connection.

This is the code:

function getConnected()
{
    $host = 'localhost';
    $user = 'logintest';
    $pass = 'logintest';
    $db = 'vibo';

    $mysqli = new mysqli($host, $user, $pass, $db);

    if ($mysqli->connect_error) {
        die('Connect Error (' . mysqli_connect_errno() . ') '
            . mysqli_connect_error());
    }
}

1 Answer

0 votes
by (8.7k points)

let’s have a look how to make mysqli connect function:

function connected_demo($host,$user,$pass,$db) {

 

   $mysqli = new mysqli($host, $user, $pass, $db);

 

   if($mysqli->connect_error) 

     die('Connect Error (' . mysqli_connect_errno() . ') '. mysqli_connect_error());

 

   return $mysqli;

}

It is  the function returning the mysqli instance.

Function call:

$mysqli = Connected_demo('localhost','user','password','database');

Related questions

0 votes
4 answers
asked Feb 27, 2021 in SQL by adhiraj (4k points)
0 votes
1 answer
asked Feb 15, 2021 in SQL by RohitSingh (2.6k points)
0 votes
1 answer
asked Dec 22, 2020 in SQL by Appu (6.1k points)
0 votes
1 answer
asked Nov 19, 2020 in SQL by Vamsee Krishna (22.5k points)
0 votes
1 answer
asked Jan 2, 2021 in SQL by Appu (6.1k points)

Browse Categories

...