<?php
header("Content
//mysqli操作mysql數據庫(面向對象方式)
//
$mysqli =new MySQLi("localhost"
if($mysqli
die("連接失敗"
}
//
$sql="select *from user
//
$res =$mysqli
//var_dump($res);
//fetch_assoc fetch_array fetch_object
while($row=$res
var_dump($row);
/* foreach($row as $val){
echo
}
echo
}
//
$res
$mysqli
?>
下面是面向過程的
<?php
header("Content
$mysqli=mysqli_connect("localhost"
if(!$mysqli){
die("連接失敗"
}
$sql="select *from user
$res=mysqli_query($mysqli
//var_dump($res);
while($row=mysqli_fetch_row($res)){
foreach ($row as $val){
echo
}
echo
}
//釋放內存
mysqli_free_result($res);
mysqli_close($mysqli);
?>
<?php
//使用mysqli 擴展庫對mysql的crud 操作
header("Content
$mysqli = new MySQLi("localhost"
if($mysqli
die("連接失敗"
}
//增加一條記錄
//$sql="insert into user
//刪除一條記錄
//$sql="delete from user
//更新一條記錄
$sql="update user
$res=$mysqli
if(!$res){
echo "操作失敗"
}else{
if($mysqli
echo "成功";
}else{
echo "沒有行受影響";
}
}
//關閉資源
$mysqli
?>
<?
class SqlHelper{
private $mysqli;
//這裡先寫死
private static $host="localhost";
private static $user="root";
private static $pwd="root";
private static $db="test";
public function __construct(){
$this
if($this
die("連接失敗"
}
//設置字符集
$this
}
//dql operate
function execute_dql($sql){
$res =$this
return $res;
}
//dml operate
function execute_dml($sql){
$res =$this
if(!$res){
return
}else{
if($this
return
}else{
return
}
}
}
}
?>
From:http://tw.wingwit.com/Article/program/PHP/201311/21284.html