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

      }

    ?>

  • 相关阅读:
    关于CoreData的使用
    【转】向iOS开发者介绍C++(-)
    Storyboard里面的几种Segue区别及视图的切换:push,modal,popover,replace和custom
    【转】Object-C 多线程中锁的使用-NSLock
    写了半天的返回
    oracle 锁表问题
    LINQ的基本认识
    Oracle客户端配置
    REVERSE
    vchar2和nvchar2
  • 原文地址:https://www.cnblogs.com/forerver-elf/p/5231941.html
Copyright © 2011-2022 走看看