Mysqli & PDO

Source: Internet
Author: User
Tags getmessage

Mysqli

<?PHP$servername= "localhost";
$username= "username";
$password= "Password"; //Create a connection
$conn=NewMysqli ($servername,$username,$password); //Detecting Connections
if($conn-connect_error) {
     die("Connection failed:".)$conn-connect_error); } 
Echo"Connection succeeded";
?>
Pdo
<?PHP$servername= "localhost";
$username= "username";
$password= "Password"; Try {
    $conn=NewPDO ("mysql:host=$servername;d Bname=mydb ",$username,$password); Echo"Connection succeeded"; }
Catch(pdoexception$e)
{
    Echo $e-getMessage ();} ?>

Close link

Example (Mysqli- object oriented)
$conn->close ();
Example (MYSQLI- process-oriented
)mysqli_close($conn);
instance (PDO)
 $conn null;

Create a database

Example (Mysqli-Object-oriented)<?PHP$servername= "localhost";
$username= "username";
$password= "Password"; //Create a connection
$conn=NewMysqli ($servername,$username,$password); //Detecting Connections
if($conn-connect_error) {
     die("Connection failed:".)$conn-connect_error); } 

//Create a database
$sql= "CREATE DATABASE MyDB";
if($conn->query ($sql) ===TRUE) {
    Echo"Database Creation succeeded";
} Else {
    Echo"Error Creating Database:".$conn-error;} $conn-close ();?>
Example (mysqli procedural)<?PHP$servername= "localhost";
$username= "username";
$password= "Password"; //Create a connection
$conn=Mysqli_connect($servername,$username,$password); //Detecting Connections
if(!$conn) {
     die("Connection failed:".)Mysqli_connect_error()); }

//Create a database
$sql= "CREATE DATABASE MyDB";
if(Mysqli_query($conn,$sql)) {
    Echo"Database Creation succeeded";
} Else {
    Echo"Error Creating Database:".Mysqli_error($conn); }

Mysqli_close($conn); ?>

Using PDO:<?PHP$servername= "localhost"; $username= "username"; $password= "Password"; Try { $conn=NewPDO ("mysql:host=$servername;d Bname=mydb ",$username,$password); //Set PDO error mode to exception $conn->setattribute (Pdo::attr_errmode, PDO::errmode_exception); $sql= "CREATE DATABASE mydbpdo"; //use EXEC (), because no results are returned $conn-exec($sql); Echo"Database Creation Success <br>"; } Catch(pdoexception$e) { Echo $sql. "<br>".$e-GetMessage (); } $conn=NULL; ?>

Mysqli & PDO

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.