Computers And Industry

Open MySql Connection Using PHP


The following example shows how to open a connection to a MySql Database using PHP.

Using the Class


host = "your.database.hostname";
$cnObj->db = "DatabaseName";
$cnObj->user = "UserName";
$cnObj->password = "UserPassword";
if ($cnObj->Open())
{
   $cn = $cnObj->cn;
   $cnObj->Close();
}

?>



The pfsConnection class

This is the connection class. It provides an open and close method and also provides date formatting routines to support the datetime type.

cn = new mysqli($this->host, $this->user, 
                    $this->password, $this->db);
    $this->status = 0;

    if (!$this->cn->connect_error)
    {
      $this->status = 1;
    }

    return $this->cn;

   }


  // -------------------------------------
  // closes the connection
  // -------------------------------------
  public function Close()
  {
    if (isset($this->cn))
    {
      mysqli_close($this->cn);
    }
  }


  // --------------------------------------
  // Gets last inserted id on connection
  // --------------------------------------
  public function GetLastInsertedID()
  {
    return mysqli_insert_id($this->cn);
  }

}

?>


Open Source

Paul F. Sirpenski
Personal Open Source Directory Of Paul F. Sirpenski

ASP.NET Core
Open Source directory Of the Microsoft Asp.Net Core project.

Developed By Paul F. Sirpenski. Copyright 2021.