1. 程式人生 > >php 操作資料庫

php 操作資料庫

推薦使用pdo或mysqli面向物件方式

1. MySQLi - 面向物件

<?php

    //使用面向物件進行資料庫的連線,在建立物件的時候就自動的連線資料
    $mySQLi = new MySQLi('localhost','root','123456','test',3306);

    //判斷資料庫是否連線
    if($mySQLi -> connect_errno){
        die('連線錯誤' . $mySQLi -> connect_error);
    }
    //設定字符集
    $mySQLi -> set_charset('utf8'
)
; //編寫sql語句並執行 $sql = "select * from good"; //傳送sql語句並執行,如果是select語句,返回的是一個物件,其他的返回來一個boolean. $res = $mySQLi -> query($sql); echo '<pre>'; //使用$res物件裡面的fetch_assoc()取出裡面的資料. // while($row = $res->fetch_assoc()){ // var_dump($row); // } // 使用fetch_row()方法 // while($row
= $res -> fetch_row())
{ // var_dump($row); // } //使用fetch_array(); // while($row = $res -> fetch_array()){ // var_dump($row); // } //fetch_object(); while($row = $res -> fetch_object()){ var_dump($row); } $res -> free(); $mySQLi -> close(); ?>

2. PDO連線操作MySQL資料庫

header("content-type:text/html;charset=utf-8");
$dsn="mysql:dbname=test;host=localhost";
$db_user='root';
$db_pass='admin';
try{
 $pdo=new PDO($dsn,$db_user,$db_pass);
}catch(PDOException $e){
 echo '資料庫連線失敗'.$e->getMessage();
}
//新增
$sql="insert into buyer (username,password,email) values ('ff','123456','[email protected]')";
$res=$pdo->exec($sql);
echo '影響行數:'.$res;

//修改
$sql="update buyer set username='ff123' where id>3";
$res=$pdo->exec($sql);
echo '影響行數:'.$res;
//查詢
$sql="select * from buyer";
$res=$pdo->query($sql);
foreach($res as $row){
 echo $row['username'].'<br/>';
}
//刪除
$sql="delete from buyer where id>5";
$res=$pdo->exec($sql);
echo '影響行數:'.$res;

3. MySQLi - 面向過程

 <?php


/**資料庫配置*/
$mysql_server_name = "localhost"; //改成自己的mysql資料庫伺服器
$mysql_username = "root"; //改成自己的mysql資料庫使用者名稱
$mysql_password = ""; //改成自己的mysql資料庫密碼
$mysql_database = "db2"; //改成自己的mysql資料庫名
$mysql_table = "person"; //改成自己的表名

/**
 * 連線資料庫
 */
$con = mysqli_connect($mysql_server_name, $mysql_username, $mysql_password); //連線資料庫
if (!$con) {
    die('Could not connect: ' . mysqli_error($con));
}
/**
 * 刪除資料庫:db2
 */
$sql_delete_db = "drop database $mysql_database";
if (mysqli_query($con, $sql_delete_db)) {
    println("$sql_delete_db ok");
} else {
    println("$sql_delete_db failed:" . mysqli_error($con));
}

/**
 * 建立資料庫:db2
 */
$sql_create_db = "create database $mysql_database";
if (mysqli_query($con, $sql_create_db)) {
    println("create ok");
} else {
    println("create failed:" . mysqli_error($con));
}

/**
 * 選擇資料庫;db2
 */
mysqli_select_db($con, $mysql_database);

/**
 * 建立資料表;person
 */
$sql_create_table = "create table $mysql_table(id int NOT NULL AUTO_INCREMENT,PRIMARY KEY(id),name varchar(15),age int)";
if (mysqli_query($con, $sql_create_table)) {
    println("create table ok");
} else {
    println("create table failed:" . mysqli_error($con));
}
/**
 * 從表(person)中刪除資料;
 */
$sql_delete = "delete from $mysql_table where age = 200";
if (mysqli_query($con, $sql_delete)) {
    println("delete table ok");
} else {
    println("delete table failed:" . mysqli_error($con));
}

/**
 * 在表(person)中插入新資料;
 */
$age = rand(12, 80);//隨機生成年齡
$sql_inset = "insert into $mysql_table (name,age) value ('flying_$age',$age)";
if (mysqli_query($con, $sql_inset)) {
    println("insert table ok");
} else {
    println("insert table failed:" . mysqli_error($con));
}
/**
 * 從表(person)中查詢資料;
 */
$sql_select = "select * from  $mysql_table order by age";
$result = mysqli_query($con, $sql_select);
/**   輸出查詢結果   */
while ($row = mysqli_fetch_array($result)) {
    println($row['id'] . " " . $row['name'] . " " . $row['age']);
}
$result->close();

/**
 * 更新表(person)中資料;
 */
$sql_update = "update $mysql_table set age = 200 where age < 67";
$result = mysqli_query($con, $sql_update);
println($result);
if ($result) {
    println("sql_update table ok");
} else {
    println("sql_update table failed:" . mysqli_error($con));
}
/**
 * 關閉資料庫連線
 */
mysqli_close($con);

連線在指令碼執行完畢後會自動關閉,也可以使用程式碼關閉連線:

MySQLi - 面向物件 $conn->close();

MySQLi - 面向過程 mysqli_close($conn);

PDO $conn = null;