php下操作mysql详解之初级!(面向对象,面向过程)
2013-11-11 14:07
761 查看
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),'郭
路@sohu.com',24);
![](http://guojiping.blog.51cto.com/attachment/201311/11/5635432_13841495359RWu.png)
![](http://guojiping.blog.51cto.com/attachment/201311/11/5635432_1384149535qrfR.png)
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); //要不要无所谓
?>
![](http://guojiping.blog.51cto.com/attachment/201311/11/5635432_1384149536RRlE.png)
![](http://guojiping.blog.51cto.com/attachment/201311/11/5635432_1384149537vZhC.png)
Sql语句换成删除:
$sql="delete from user where id=4";
![](http://guojiping.blog.51cto.com/attachment/201311/11/5635432_1384149538cIFP.png)
$sql="update user set age=26 where name='lzw'";
![](http://guojiping.blog.51cto.com/attachment/201311/11/5635432_13841495389MYV.png)
将上面的文件,封装成类,提高复用性!
两个文件:
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;
}
}
}
}
?>
![](http://guojiping.blog.51cto.com/attachment/201311/11/5635432_1384149540zX5B.png)
命令改为$sql="delete from user where id=3";
![](http://guojiping.blog.51cto.com/attachment/201311/11/5635432_1384149541rm6H.png)
//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);
执行结果如下:
![](http://guojiping.blog.51cto.com/attachment/201311/11/5635432_1384149543DCMa.png)
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();
?>
![](http://guojiping.blog.51cto.com/attachment/201311/11/5635432_13841495448sGA.png)
由于上面少了这个$msi->query("set names utf8");所以出现汉字的乱码
![](http://guojiping.blog.51cto.com/attachment/201311/11/5635432_1384149545qu4Q.png)
正常了!
例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);
?>
![](http://guojiping.blog.51cto.com/attachment/201311/11/5635432_1384149546hW0m.png)
![](http://guojiping.blog.51cto.com/attachment/201311/11/5635432_13841495498dr7.png)
如何区分这几个?
通过上面的程序修改演示:
while($row=mysqli_fetch_row($res)){
print "<pre>";
var_dump($row);
print "</pre>";
echo "<br/>";
}
![](http://guojiping.blog.51cto.com/attachment/201311/11/5635432_1384149550BsEf.png)
上面是以下标
while($row=mysqli_fetch_assoc($res)){
print "<pre>";
var_dump($row);
print "</pre>";
echo "<br/>";
}
![](http://guojiping.blog.51cto.com/attachment/201311/11/5635432_1384149551idFv.png)
上面是以字段名,如id
![](http://guojiping.blog.51cto.com/attachment/201311/11/5635432_1384149552rySP.png)
![](http://guojiping.blog.51cto.com/attachment/201311/11/5635432_1384149553i30E.png)
例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();
?>
![](http://guojiping.blog.51cto.com/attachment/201311/11/5635432_1384149554DqUd.png)
执行了2次,添加了2条
![](http://guojiping.blog.51cto.com/attachment/201311/11/5635432_1384149555VZC8.png)
把sql语句改为:(不存在的id)
$sql="update user set name='haha' where id=11";
![](http://guojiping.blog.51cto.com/attachment/201311/11/5635432_1384149556899n.png)
$sql="update user set name='haha' where id=9";
![](http://guojiping.blog.51cto.com/attachment/201311/11/5635432_1384149557aCu0.png)
$sql="delete from user where name='haha'";
增删改都使用了,这里我们来封装成工具类
![](http://guojiping.blog.51cto.com/attachment/201311/11/5635432_1384149558eR2G.png)
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 "没有受影响的行数";
}
}
?>
![](http://guojiping.blog.51cto.com/attachment/201311/11/5635432_1384149558G9B4.png)
操作数据库成功如下:
![](http://guojiping.blog.51cto.com/attachment/201311/11/5635432_1384149560bS0q.png)
-> 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),'郭
路@sohu.com',24);
![](http://guojiping.blog.51cto.com/attachment/201311/11/5635432_13841495359RWu.png)
![](http://guojiping.blog.51cto.com/attachment/201311/11/5635432_1384149535qrfR.png)
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); //要不要无所谓
?>
![](http://guojiping.blog.51cto.com/attachment/201311/11/5635432_1384149536RRlE.png)
![](http://guojiping.blog.51cto.com/attachment/201311/11/5635432_1384149537vZhC.png)
Sql语句换成删除:
$sql="delete from user where id=4";
![](http://guojiping.blog.51cto.com/attachment/201311/11/5635432_1384149538cIFP.png)
$sql="update user set age=26 where name='lzw'";
![](http://guojiping.blog.51cto.com/attachment/201311/11/5635432_13841495389MYV.png)
将上面的文件,封装成类,提高复用性!
两个文件:
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;
}
}
}
}
?>
![](http://guojiping.blog.51cto.com/attachment/201311/11/5635432_1384149540zX5B.png)
命令改为$sql="delete from user where id=3";
![](http://guojiping.blog.51cto.com/attachment/201311/11/5635432_1384149541rm6H.png)
//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);
执行结果如下:
![](http://guojiping.blog.51cto.com/attachment/201311/11/5635432_1384149543DCMa.png)
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();
?>
![](http://guojiping.blog.51cto.com/attachment/201311/11/5635432_13841495448sGA.png)
由于上面少了这个$msi->query("set names utf8");所以出现汉字的乱码
![](http://guojiping.blog.51cto.com/attachment/201311/11/5635432_1384149545qu4Q.png)
正常了!
例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);
?>
![](http://guojiping.blog.51cto.com/attachment/201311/11/5635432_1384149546hW0m.png)
![](http://guojiping.blog.51cto.com/attachment/201311/11/5635432_13841495498dr7.png)
如何区分这几个?
通过上面的程序修改演示:
while($row=mysqli_fetch_row($res)){
print "<pre>";
var_dump($row);
print "</pre>";
echo "<br/>";
}
![](http://guojiping.blog.51cto.com/attachment/201311/11/5635432_1384149550BsEf.png)
上面是以下标
while($row=mysqli_fetch_assoc($res)){
print "<pre>";
var_dump($row);
print "</pre>";
echo "<br/>";
}
![](http://guojiping.blog.51cto.com/attachment/201311/11/5635432_1384149551idFv.png)
上面是以字段名,如id
![](http://guojiping.blog.51cto.com/attachment/201311/11/5635432_1384149552rySP.png)
![](http://guojiping.blog.51cto.com/attachment/201311/11/5635432_1384149553i30E.png)
例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();
?>
![](http://guojiping.blog.51cto.com/attachment/201311/11/5635432_1384149554DqUd.png)
执行了2次,添加了2条
![](http://guojiping.blog.51cto.com/attachment/201311/11/5635432_1384149555VZC8.png)
把sql语句改为:(不存在的id)
$sql="update user set name='haha' where id=11";
![](http://guojiping.blog.51cto.com/attachment/201311/11/5635432_1384149556899n.png)
$sql="update user set name='haha' where id=9";
![](http://guojiping.blog.51cto.com/attachment/201311/11/5635432_1384149557aCu0.png)
$sql="delete from user where name='haha'";
增删改都使用了,这里我们来封装成工具类
![](http://guojiping.blog.51cto.com/attachment/201311/11/5635432_1384149558eR2G.png)
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 "没有受影响的行数";
}
}
?>
![](http://guojiping.blog.51cto.com/attachment/201311/11/5635432_1384149558G9B4.png)
操作数据库成功如下:
![](http://guojiping.blog.51cto.com/attachment/201311/11/5635432_1384149560bS0q.png)
相关文章推荐
- MySQL存储过程详解和PHP操作示例
- 详解 CentOS 安装配置 Nginx 0.8.33+PHP 5.3.1+php-fpm+MySQL 全过程【转】
- php mysql操作mysql_connect连接数据库实例详解
- php调用mysql存储过程的详解
- 用PHP和MYSQL建立计数器过程详解
- PHP : MySQLi【面向过程】操作数据库【 连接、建库、建表、增、删、改、查、关闭】
- 用PHP和MYSQL建立计数器过程详解
- MySQL_面向过程访问.php
- Ubuntu下Apache+PHP+MySQL安装过程详解
- 用PHP和MYSQL建立计数器过程详解
- 基于Php mysql存储过程的详解
- mysql存储过程详解以及PHP调用MYSQL存储过程实例
- 数组面向过程与面向对象的基本操作
- 基于Php mysql存储过程的详解
- 基于apache + mysql + php编译安装过程详解
- PHP中的面向对象和面向过程
- PHP操作mysql函数详解,mysql和php交互函数
- PHP中的面向对象和面向过程 推荐
- uva 492 Pig_Latin 题目详解及面向过程,面向对象的编程思想的粗略讲解