天天看点

php下操作mysql详解之初级!(面向对象,面向过程)

mysql> create table user(

    -> id int primary key auto_increment,

    -> name varchar(32) not null,

    -> password varchar(64) not null,

    -> email varchar(128) not null,

    -> age tinyint unsigned not null

-> );

mysql> insert into user (name,password,email,age) values("gjp",md5(123456),'gjp@

sohu.com',24);

mysql> insert into user (name,password,email,age) values("郭卢",md5(123456),'郭

<a href="http://guojiping.blog.51cto.com/attachment/201311/11/5635432_1384149535xxQw.png"></a>

<a href="http://guojiping.blog.51cto.com/attachment/201311/11/5635432_1384149535OzRU.png"></a>

Mysql客户端的限制,只能接受gbk码,utf8不支持,数据库是支持的

&lt;?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'),'[email protected]',34)";

$res=mysql_query($sql,$conn);

if(!$res)

echo "操作失败";

if (mysql_affected_rows($conn)&gt;0)

echo "操作成功!";

}else{ 

echo "没有受影响的行数!";

mysql_close($conn); //要不要无所谓

?&gt;

<a href="http://guojiping.blog.51cto.com/attachment/201311/11/5635432_13841495368IVF.png"></a>

<a href="http://guojiping.blog.51cto.com/attachment/201311/11/5635432_13841495367rJp.png"></a>

Sql语句换成删除:

$sql="delete  from user where id=4";

<a href="http://guojiping.blog.51cto.com/attachment/201311/11/5635432_1384149537pdwt.png"></a>

$sql="update user set age=26 where name='lzw'";

<a href="http://guojiping.blog.51cto.com/attachment/201311/11/5635432_1384149538jn4B.png"></a>

将上面的文件,封装成类,提高复用性!

两个文件:

index.php

1. &lt;?php

2. require_once 'Sqltool.php';

3. $sql="insert into user(name,password,email,age)  values('lisi',md5('123'),'[email protected]',36)";

4. $sqlTool=new Sqltool();

5. $res=$sqlTool-&gt;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. ?&gt;

Sqltool.php

class Sqltool

private $conn;

private $host="localhost";

private $user="root";

private $password="123456";

private $db="test";

function Sqltool()

  {

    $this-&gt;conn=mysql_connect($this-&gt;host,$this-&gt;user,$this-&gt;password);

if(!$this-&gt;conn)

die("fail".mysql_error());

mysql_select_db($this-&gt;db,$this-&gt;conn);

    mysql_query("set names utf8");

  }

//dql 针对select

public function execute_dql($sql)

$res=mysql_query($sql,$this-&gt;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-&gt;conn);

if(!$b)

    {

return 0;

    }else {

if(mysql_affected_rows($this-&gt;conn)&gt;0)

return 1;

}else{

return 2;

    }

<a href="http://guojiping.blog.51cto.com/attachment/201311/11/5635432_1384149539ZbdB.png"></a>

命令改为$sql="delete from user where id=3";

<a href="http://guojiping.blog.51cto.com/attachment/201311/11/5635432_1384149540cXwl.png"></a>

//DQL语句获取的是结果集,执行这的时候,要将dml语句先注释掉

$sql="select * from user";

$sqlTool=new Sqltool();

$res=$sqlTool-&gt;execute_dql($sql);

while($row=mysql_fetch_row($res))

foreach ($row as $key=&gt;$val)

echo "--$val";

echo "&lt;br/&gt;";

mysql_free_result($res);

执行结果如下:

<a href="http://guojiping.blog.51cto.com/attachment/201311/11/5635432_1384149542Y0nd.png"></a>

MYSQli的讲解2!

在php.ini 中开启

extension=php_mysqli.dll

例1:使用面向对象的方式

//header("Content-type: text/html; charset=utf-8");

//1.创建mysql对象

$msi=new mysqli("localhost","root","123456","test");

//验证是否ok

if($msi-&gt;cononnect_error){

die("连接失败".$msi-&gt;connect_error);

}else {

echo "连接ok"."&lt;/br&gt;";

//2.操作数据库(发送sql命令)

$sql="select * from user ";

$res=$msi-&gt;query($sql);

/* echo "&lt;/br&gt;";

print "&lt;pre&gt;";

var_dump($res);

print"&lt;/pre&gt;"; */

//3.处理结果

while($row=$res-&gt;fetch_row()){

foreach ($row as $key=&gt;$val){

//4.关闭资源

//释放内存;

$res-&gt;free();

//关闭链接

$msi-&gt;close();

<a href="http://guojiping.blog.51cto.com/attachment/201311/11/5635432_1384149543B6mU.png"></a>

由于上面少了这个$msi-&gt;query("set names utf8");所以出现汉字的乱码

<a href="http://guojiping.blog.51cto.com/attachment/201311/11/5635432_13841495440Anq.png"></a>

正常了!

例2:用面向过程的方法

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)

$res=mysqli_query($msi,$sql);

//var_dump($res);

//3.处理得到的结果

//循环取出结果集

while($row=mysqli_fetch_row($res)){

foreach($row as $key=&gt;$val){

mysqli_free_result($res);

mysqli_close($msi);

<a href="http://guojiping.blog.51cto.com/attachment/201311/11/5635432_1384149545ca8e.png"></a>

<a href="http://guojiping.blog.51cto.com/attachment/201311/11/5635432_1384149547rM9Y.png"></a>

如何区分这几个?

通过上面的程序修改演示:

var_dump($row);

print "&lt;/pre&gt;";

<a href="http://guojiping.blog.51cto.com/attachment/201311/11/5635432_1384149549DAkb.png"></a>

上面是以下标

while($row=mysqli_fetch_assoc($res)){

<a href="http://guojiping.blog.51cto.com/attachment/201311/11/5635432_1384149551H054.png"></a>

上面是以字段名,如id

<a href="http://guojiping.blog.51cto.com/attachment/201311/11/5635432_1384149552flYb.png"></a>

<a href="http://guojiping.blog.51cto.com/attachment/201311/11/5635432_13841495538BM5.png"></a>

例3:

$msi-&gt;query("set names utf8");

$sql="insert into user  (name,password,email,age) values('李想',md5('aaaa'),'[email protected]',18)";

if(!$res){

echo "操作失败".$msi-&gt;error;

//影响多少行记录

if($msi-&gt;affected_rows&gt;0){

echo "执行ok";

echo "没有受影响的行数";

<a href="http://guojiping.blog.51cto.com/attachment/201311/11/5635432_1384149553IpQr.png"></a>

执行了2次,添加了2条

<a href="http://guojiping.blog.51cto.com/attachment/201311/11/5635432_1384149555i5dt.png"></a>

把sql语句改为:(不存在的id)

$sql="update  user set name='haha' where id=11";

<a href="http://guojiping.blog.51cto.com/attachment/201311/11/5635432_1384149555CpCK.png"></a>

$sql="update  user set name='haha' where id=9";

<a href="http://guojiping.blog.51cto.com/attachment/201311/11/5635432_1384149557sqEV.png"></a>

$sql="delete from user where name='haha'";

增删改都使用了,这里我们来封装成工具类

<a href="http://guojiping.blog.51cto.com/attachment/201311/11/5635432_13841495579HxM.png"></a>

Mysqli封装为工具类sqlHelper:

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-&gt;mysqli=new  mysqli(self::$host,self::$user,self::$pwd,self::$db);

if($this-&gt;mysqli-&gt;connect_error){

die("连接失败".$this-&gt;mysqli-&gt;connect_error);

echo "success";  //为了调试

//设置访问数据库的字符集,保证php是以utf8的方式来操作我们的mysql数据库

$this-&gt;mysqli-&gt;query("set names utf8");

public function execute_dql($sql){

$res=$this-&gt;mysqli-&gt;query($sql) or die("操作dql".$this-&gt;mysqli-&gt;error);

return  $res;

public function execute_dml($sql){

echo $sql; //为了调试

$res=$this-&gt;mysqli-&gt;query($sql) or die("操作

dql".$this-&gt;mysqli-&gt;error);

echo $res;//为了调试

if($this-&gt;mysqli-&gt;affected_rows&gt;0){

使用工具类,实现其功能:

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'),'[email protected]','8')";

$sqlhelper-&gt;_construct();

$res=$sqlhelper-&gt;execute_dml($sql);

if($res==0){

echo "失败";

if($res==1){

echo "恭喜,成功!";

<a href="http://guojiping.blog.51cto.com/attachment/201311/11/5635432_1384149558j4Cu.png"></a>

操作数据库成功如下:

<a href="http://guojiping.blog.51cto.com/attachment/201311/11/5635432_1384149559OUDz.png"></a>

本文转自 gjp0731 51CTO博http://blog.51cto.com/guojiping/1323187客,原文链接: