Connection to DB

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Connection to DB相关的知识,希望对你有一定的参考价值。

Connect to mysql

<!-- MySQLi Object-Oriented -->

<?php

  $servername = "localhost";

  $username = "username";

  $password = "pwd";

  

  //create  connection

  $conn = new mysqli($servername, $username, $password);

 

  //check connection

  if($conn-> conncet_error){

    die("Connection failed:" .$conn->connect_error);

  }

 

  echo "Connected successfully";

 

  $conn->close();

?>

 

<!-- MySQLi Procedual -->

<?php

  $servername = "localhost";

  $username = "username";

  $password = "pwd";

 

  //create connection

  $conn = mysqli_connect($servername, $username, $password);

 

  //check connection

  if(!$conn){

    die("Connection failed:" .mysqli_connect_error());

  }

  echo "Connected successfully";

 

  mysql_close($conn);

?>

 

<!-- PDO -->

<?php

  $servername = "localhost";

  $username = "username";

  $password = "pwd";

  try{

    $conn = new PDO("mysql:host=$servername;dbname=myDB", $username, $password);

    //set the PDO error mode to exception

    $conn ->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    echo "Connection successfully";

    $conn = null;

  }

  catch(PDOException $e){

    echo "Connection failed:" .$e->getMessage();

  }

?>

以上是关于Connection to DB的主要内容,如果未能解决你的问题,请参考以下文章

fail to get a free connection

IntelliJ IDEA连接不上数据库 (Connection to testdb@localhost failed. [08001] Could not create connection to

IntelliJ IDEA连接不上数据库 (Connection to testdb@localhost failed. [08001] Could not create connection to

安装Kubernetes报错:The connection to the server localhost:8080 was refused

phpstudy mysql 命令行 No connection. Trying to reconnect... Connection id: 22 Current database: **

MYSQL:Lost connection to MySQL server at 'reading initial communication packet', system erro