Hackerss.com is a community of amazing hackers

Hackerss is a community for developers, data scientitst, ethical hackers, hardware enthusiasts or any person that want to learn / share their knowledge of any aspect of digital technology.

Create account Log in

Posted on

Php Connect to mysql database


 * function to connect to mysql database with comments
 * @param string $host  Hostname of the database
 * @param string $user  Username of the database
 * @param string $pass  Password of the database
 * @param string $dbname  Name of the database
 * @return $conn
function connectToDB($host, $user, $pass, $dbname) {
  // create connection
  $conn = new mysqli($host, $user, $pass, $dbname);

  // check connection
  if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);

  // return connection
  return $conn;

$conn = connectToDB("localhost", "root", "", "comments");

$conn; //object(mysqli)#1 (0) { }
Enter fullscreen mode Exit fullscreen mode

Top comments (0)