Mysql connection
PDO
<?php $servername = "localhost"; $username = "username"; $password = "password"; try{ $conn = new PDO("mysql:host=$servername;dbname=myDB",$username,$password); echo "连接成功"; } catch(PDOException $e){ echo $e->getMessage(); } ?>
Close MySQL connection
Example (MySQLi - Object Oriented) $ conn-> close ();
Example (MySQLi - process-oriented) mysqli_close ($ conn);
Examples of (PDO) $ conn = null;
Create a database to create a table
MySQLi (object-oriented oop): $ conn-> query ($ sql)
MySQLi (process-oriented pop): mysqli_query ($ conn, $ sql)
PDO: PDO Object -> query ($ sql)
? < PHP header ( "Content-of the type: text / HTML; charset = UTF-8"); // set the encoding $ ServerName = "localhost" ; $ username = "root" ; $ password = "root" ; // create connecting $ Conn = mysqli_connect ( $ ServerName , $ username , $ [assword); // detect a connection IF (! $ Conn ) { Die ( "connection failed:." mysqli_connect_error ()); } // Create database $ SQL = " Database myDB drop " ; IF ( mysqli_query ( $ conn , $ SQL )) { echo "database deleted successfully" ; } the else { echo "database deletion failed:." mysqli_error ( $ conn ); } mysqli_close ( $ conn );
MySQL inserting data
Insert a single data: Local execute SQL statements
pop : mysqli_query()
oop:$oop->query()
PDO:$PDO->query()
Insert multiple data
pop:mysqli_multi_query ($con,$sql)
oop:$oop->multi_query()
PDO: $ PDO-> exec () a plurality of data using transactions, or insertion cycle, returns with the error information into the data.