zoukankan      html  css  js  c++  java
  • Connection to DB

    Connect to MySQL

    PHP5 and later can work with a MySQL database using 

    •   MySQLi extension
    •   PDO

    PDO will work on 12 different database systems, where as MySQLi will only work with MySQL database.

    <!-- 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();

      }

    ?>

  • 相关阅读:
    函数二 10
    函数初识 09
    文件操作 08
    数据类型的补充 day07
    小数据池 深浅copy 集合
    python Mysql 多条件查询
    ElasticSearch Python 基本操作
    用PyInstaller把Python代码打包成单个独立的exe可执行文件
    python 编译EXE文件
    Git 创建新分支检查分支
  • 原文地址:https://www.cnblogs.com/forerver-elf/p/5231941.html
Copyright © 2011-2022 走看看