samedi 2 février 2019

How to stay connected with MySQLi from php

I'm setting up a new website and i want to enter some registrations in my MySQLi database.

I want to stay connected with the DB and not to making a new connection every time in a sub-page.

In the Home page i'm starting the connection like this:

$con = mysqli_connect("localhost", "root", "", "Website");     
    if (!$con)    {                                                   
    echo "Error: Unable to connect to MySQL.";
    exit;         }
    if($con){       echo "Successful connect";}

The Home page have some href that navigate to other php pages. For example:

 <a href="/InsertInDatabase.php">Insert</a>
 <a href="/SearchDatabase.php">Search</a>
 <a href="/DeletefromDatabase.php">Delete</a>

Into every php now i'm making again a connection with the DB

InsertInDatabase.php

    $con = mysqli_connect("localhost", "root", "", "Website");     
    if (!$con)    {                                                   
    echo "Error: Unable to connect to MySQL.";
    exit;         }
    if($con){       echo "Successful connect";}

SearchDatabase.php

    $con = mysqli_connect("localhost", "root", "", "Website");     
    if (!$con)    {                                                   
    echo "Error: Unable to connect to MySQL.";
    exit;         }
    if($con){       echo "Successful connect";}

and I do this for every new php..... Is there a way to stay connected from HomePage.php and not to do this every time again and again?




Aucun commentaire:

Enregistrer un commentaire