mysql> create table user(php
-> id int primary key auto_increment,html
-> name varchar(32) not null,mysql
-> password varchar(64) not null,sql
-> email varchar(128) not null,數據庫
-> age tinyint unsigned not nullide
-> );函數
mysql> insert into user (name,password,email,age) values("gjp",md5(123456),'gjp@工具
sohu.com',24);fetch
mysql> insert into user (name,password,email,age) values("郭盧",md5(123456),'郭ui
Mysql客戶端的限制,只能接受gbk碼,utf8不支持,數據庫是支持的
<?php
$conn=mysql_connect("localhost","root","123456");
if(!$conn)
{
die("出錯了".mysql_errno());
}
mysql_select_db("test",$conn) or die(mysql_errno());
mysql_query("set names utf8");
$sql="insert into user (name,password,email,age) values('zhangsan',md5('123'),'zs@163.com',34)";
$res=mysql_query($sql,$conn);
if(!$res)
{
echo "操做失敗";
}
if (mysql_affected_rows($conn)>0)
{
echo "操做成功!";
}else{
echo "沒有受影響的行數!";
}
mysql_close($conn); //要不要無所謂
?>
Sql語句換成刪除:
$sql="delete from user where id=4";
$sql="update user set age=26 where name='lzw'";
將上面的文件,封裝成類,提升複用性!
兩個文件:
index.php
1. <?php
2. require_once 'Sqltool.php';
3. $sql="insert into user(name,password,email,age) values('lisi',md5('123'),'ls@163.com',36)";
4. $sqlTool=new Sqltool();
5. $res=$sqlTool->execute_dml($sql);
6. if($res==0){
7. echo "失敗";
8. }else if($res==1){
9. echo "success";
10. }else if($res==2){
11. echo "沒有受影響的行數";
12. }
13. ?>
Sqltool.php
<?php
class Sqltool
{
private $conn;
private $host="localhost";
private $user="root";
private $password="123456";
private $db="test";
function Sqltool()
{
$this->conn=mysql_connect($this->host,$this->user,$this->password);
if(!$this->conn)
{
die("fail".mysql_error());
}
mysql_select_db($this->db,$this->conn);
mysql_query("set names utf8");
}
//dql 針對select
public function execute_dql($sql)
{
$res=mysql_query($sql,$this->conn)or die(mysql_error());
return $res;
}
// dml語句是針對update delete insert 命令,返回值爲true false
public function execute_dml($sql)
{
echo $sql;
$b=mysql_query($sql,$this->conn);
if(!$b)
{
return 0;
}else {
if(mysql_affected_rows($this->conn)>0)
{
return 1;
}else{
return 2;
}
}
}
}
?>
命令改成$sql="delete from user where id=3";
//DQL語句獲取的是結果集,執行這的時候,要將dml語句先註釋掉
$sql="select * from user";
$sqlTool=new Sqltool();
$res=$sqlTool->execute_dql($sql);
while($row=mysql_fetch_row($res))
{
foreach ($row as $key=>$val)
{
echo "--$val";
}
echo "<br/>";
}
mysql_free_result($res);
執行結果以下:
MYSQli的講解2!
在php.ini 中開啓
extension=php_mysqli.dll
例1:使用面向對象的方式
<?php
//header("Content-type: text/html; charset=utf-8");
//1.建立mysql對象
$msi=new mysqli("localhost","root","123456","test");
//驗證是否ok
if($msi->cononnect_error){
die("鏈接失敗".$msi->connect_error);
}else {
echo "鏈接ok"."</br>";
}
//2.操做數據庫(發送sql命令)
$sql="select * from user ";
$res=$msi->query($sql);
/* echo "</br>";
print "<pre>";
var_dump($res);
print"</pre>"; */
//3.處理結果
while($row=$res->fetch_row()){
foreach ($row as $key=>$val){
echo "--$val";
}
echo "<br/>";
}
//4.關閉資源
//釋放內存;
$res->free();
//關閉連接
$msi->close();
?>
因爲上面少了這個$msi->query("set names utf8");因此出現漢字的亂碼
正常了!
例2:用面向過程的方法
<?php
header("Content-type: text/html; charset=utf-8");
//1.獲得mysqli鏈接
$msi=mysqli_connect("localhost","root","123456","test");
if(!$msi){
die("鏈接失敗".mysqli_connect_errno($msi));
}
//2.向數據庫發送sql語句(ddl dml dql)
$sql="select * from user";
$res=mysqli_query($msi,$sql);
//var_dump($res);
//3.處理獲得的結果
//循環取出結果集
while($row=mysqli_fetch_row($res)){
foreach($row as $key=>$val){
echo "--$val";
}
echo "<br/>";
}
//4.關閉資源
mysqli_free_result($res);
mysqli_close($msi);
?>
如何區分這幾個?
經過上面的程序修改演示:
while($row=mysqli_fetch_row($res)){
print "<pre>";
var_dump($row);
print "</pre>";
echo "<br/>";
}
上面是如下標
while($row=mysqli_fetch_assoc($res)){
print "<pre>";
var_dump($row);
print "</pre>";
echo "<br/>";
}
上面是以字段名,如id
例3:
<?php
//1.建立mysql對象
$msi=new mysqli("localhost","root","123456","test");
//驗證是否ok
if($msi->cononnect_error){
die("鏈接失敗".$msi->connect_error);
}else {
echo "鏈接ok"."</br>";
}
//2.操做數據庫(發送sql命令)
$msi->query("set names utf8");
$sql="insert into user (name,password,email,age) values('李想',md5('aaaa'),'lixiang@163.com',18)";
$res=$msi->query($sql);
//3.處理結果
if(!$res){
echo "操做失敗".$msi->error;
}else{
//影響多少行記錄
if($msi->affected_rows>0){
echo "執行ok";
}else{
echo "沒有受影響的行數";
}
}
//4.關閉資源
//關閉連接
$msi->close();
?>
執行了2次,添加了2條
把sql語句改成:(不存在的id)
$sql="update user set name='haha' where id=11";
$sql="update user set name='haha' where id=9";
$sql="delete from user where name='haha'";
增刪改都使用了,這裏咱們來封裝成工具類
Mysqli封裝爲工具類sqlHelper:
<?php
class sqlHelper{
private $mysqli;
private static $host="localhost";
private static $user="root";
private static $pwd="123456";
private static $db="test";
//下面這個函數__construct()實際上是兩個下劃線,下面因爲我寫了一個,因此沒法自動調用
該函數,須要手動調,若是寫成兩個_,建立對象時,則會自動調!
public function _construct(){
$this->mysqli=new mysqli(self::$host,self::$user,self::$pwd,self::$db);
if($this->mysqli->connect_error){
die("鏈接失敗".$this->mysqli->connect_error);
}else{
echo "success"; //爲了調試
}
//設置訪問數據庫的字符集,保證php是以utf8的方式來操做咱們的mysql數據庫
$this->mysqli->query("set names utf8");
}
public function execute_dql($sql){
$res=$this->mysqli->query($sql) or die("操做dql".$this->mysqli->error);
return $res;
}
public function execute_dml($sql){
echo $sql; //爲了調試
$res=$this->mysqli->query($sql) or die("操做
dql".$this->mysqli->error);
echo $res;//爲了調試
if(!$res){
return 0;
}else{
if($this->mysqli->affected_rows>0){
return 1;
}else {
return 2;
}
}
}
}
使用工具類,實現其功能:
<?php
require_once 'sqlHelper.php';
//header("Content-type: text/html;charset=utf-8");
//建立sqlHelper對象
$sqlhelper=new sqlHelper();
$sql="insert into user(name,password,email,age) values('盧偉da',md5('aaaa'),'lzw@sohu.com','8')";
$sqlhelper->_construct();
$res=$sqlhelper->execute_dml($sql);
if($res==0){
echo "失敗";
}else {
if($res==1){
echo "恭喜,成功!";
}else{
echo "沒有受影響的行數";
}
}
?>
操做數據庫成功以下: