PHP鏈接mysql數據庫:php
<?php
$server = "localhost";
$user = "root";
$pwd = "123456";
$db = "student";
try{
$conn = new PDO("mysql:host=$server;dbname=$db", $user, $pwd);
echo "鏈接成功";
} catch (PDOException $e){
echo $e->getMessage();
}mysql
PHP建立mysql數據庫:sql
<?php
$server = "localhost";
$user = "root";
$pwd = "123456";
$db = "student";
try{
$conn = new PDO("mysql:host=$server;", $user, $pwd);
// 設置 PDO 錯誤模式爲異常 ,用於拋出異常
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "create database mydb";
$conn->exec($sql);
echo "數據庫建立成功<br>";
}catch (PDOException $e){
echo $sql . "<br>" . $e->getMessage();
}
//關閉鏈接
$conn = null;數據庫
PHP建立mysql數據表:服務器
<?php
$server = "localhost";
$user = "root";
$pwd = "123456";
$db = "mydb";
try{
$conn = new PDO("mysql:host=$server;dbname=$db", $user, $pwd);
$conn -> setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
$sql = "create table myfriend(
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(30) NOT NULL ,
email VARCHAR(50),
sex CHAR(1)
)";
$conn->exec($sql);
echo "數據表建立成功";
}catch (PDOException $e){
echo $sql . "<br>" . $e->getMessage();
}
$conn = null;併發
PHP插入記錄:優化
<?php
$server = "localhost";
$user = "root";
$pwd = "123456";
$db = "mydb";
try{
$conn = new PDO("mysql:host=$server;dbname=$db", $user, $pwd);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "insert into myfriend values (1, '吳俊傑', '1132431868@qq.com', '男')";
$conn->exec($sql);
echo "數據插入成功";
}catch (PDOException $e){
echo $sql . "<br>" . $e->getMessage();
}
$conn = null;server
PHP插入多條記錄:事務
<?php
$server = "localhost";
$user = "root";
$pwd = "123456";
$db = "mydb";
try{get
$conn = new PDO("mysql:host=$server;dbname=$db", $user, $pwd);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
//開始事務
$conn->beginTransaction();
//sql語句
$conn->exec("insert into myfriend values (3,'吳俊傑','1132431868@gmail.com','男')");
$conn->exec("insert into myfriend values (4,'john','1132431868@gmail.com','男')");
$conn->exec("insert into myfriend values (5,'jack','jack1132431868@gmail.com','男')");
//提交事務
$conn->commit();
echo "多條記錄提交成功";
}catch (PDOException $e){
//若是插入失敗則回滾事務
$conn->rollBack();
echo $e->getMessage();
}
$conn = null;
MySQL 預處理語句:
預處理語句用於執行多個相同的 SQL 語句,而且執行效率更高。
預處理語句的工做原理以下:
預處理:建立 SQL 語句模板併發送到數據庫。預留的值使用參數 "?" 標記 。例如:
INSERT INTO MyGuests(firstname, lastname, email) VALUES(?,?,?)數據庫解析,編譯,對SQL語句模板執行查詢優化,並存儲結果不輸出。
執行:最後,將應用綁定的值傳遞給參數("?" 標記),數據庫執行語句。應用能夠屢次執行語句,若是參數的值不同。
相比於直接執行SQL語句,預處理語句有兩個主要優勢:
預處理語句大大減小了分析時間,只作了一次查詢(雖然語句屢次執行)。
綁定參數減小了服務器帶寬,你只須要發送查詢的參數,而不是整個語句。
預處理語句針對SQL注入是很是有用的,由於參數值發送後使用不一樣的協議,保證了數據的合法性。
<?php$server = "localhost";$user = "root";$pwd = "123456";$db = "mydb";try{ $conn = new PDO("mysql:host=$server;dbname=$db", $user, $pwd); $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); //預處理sql並綁定參數 $stmt = $conn->prepare("insert into myfriend VALUES (:id, :name, :email, :sex)"); $stmt->bindParam(':id',$id); $stmt->bindParam(':name',$name); $stmt->bindParam(':email',$email); $stmt->bindParam(':sex',$sex); //填充數據,能夠一次填充多條記錄 $id = 6; $name = "ting"; $email = '113243186@163.com'; $sex = "女"; $stmt->execute(); echo "預處理動做完成";}catch (PDOException $e){ echo $e->getMessage();}$conn = null;