当前位置: 编程技术>php
本页文章导读:
▪php mysqli扩展库操作mysql的例子 1、mysqli扩展库操作mysql的dql
<?php
header("Content-type: text/html;charset=utf-8");
//mysqli操作mysql数据库(面向对象方式)
//1、创建MySQLi对象
$mysqli =new MySQLi("localhost","root","root","test");
.........
▪php mysqli实例 使用mysqli批量执行sql语句 本节演示php中使用msyqli批量执行sql语句的例子。
1,mysqli查询,批量执行update\delete等语句
<?php
//mysqli批量执行sql语句
//批量执行dql
//使用mysqli::multi_query() 一次性添加3个用户
$mysql.........
▪php 短信接口的示例代码(入门) php 短信接口文件:
<?
/*--------------------------------
功能: PHP HTTP接口 发送短信
修改日期: 2013-09-08
说明: http://www./tx/?uid=用户账号&pwd=MD5位32密码&mobile=号码&content=内容
状态:
100 .........
[1]php mysqli扩展库操作mysql的例子
来源: 互联网 发布时间: 2013-12-24
1、mysqli扩展库操作mysql的dql
<?php
header("Content-type: text/html;charset=utf-8");
//mysqli操作mysql数据库(面向对象方式)
//1、创建MySQLi对象
$mysqli =new MySQLi("localhost","root","root","test");
if($mysqli->connect_error){
die("连接失败".$mysqli->connect_error);
}
//2、操作数据库(发送sql)
$sql="select *from user1";
//3、处理结果
$res =$mysqli->query($sql);
//var_dump($res);
//fetch_assoc \fetch_array \fetch_object
while($row=$res->fetch_row()){
var_dump($row);
/* foreach($row as $val){
echo '--'.$val;
}
echo '<br/>';*/
}
//4、关闭资源
$res->free();
$mysqli->close();
?>
面向过程的例子:
<?php
header("Content-type: text/html;charset=utf-8");
$mysqli=mysqli_connect("localhost","root","root","test");
if(!$mysqli){
die("连接失败".mysqli_connect_error());
}
$sql="select *from user1";
$res=mysqli_query($mysqli,$sql);
//var_dump($res);
while($row=mysqli_fetch_row($res)){
foreach ($row as $val){
echo '-'.$val;
}
echo '<br/>';
}
//释放内存
mysqli_free_result($res);
mysqli_close($mysqli);
?>
2、mysqli扩展库对mysql的dml操作
<?php
//使用mysqli 扩展库对mysql的crud 操作
header("Content-type: text/html;charset=utf-8");
$mysqli = new MySQLi("localhost","root","root","test");
if($mysqli->connect_error){
die("连接失败".$mysql->connect_error);
}
//增加一条记录
//$sql="insert into user1 (name,password,email,age) values ('lucy',md5('lucy'),'lucy@163.com',17)";
//删除一条记录
//$sql="delete from user1 where id =80";
//更新一条记录
$sql="update user1 set age=20 where id=7";
$res=$mysqli->query($sql);
if(!$res){
echo "操作失败".$mysqli->error;
}else{
if($mysqli->affected_rows>0){
echo "成功";
}else{
echo "没有行受影响";
}
}
//关闭资源
$mysqli->close();
?>
3、进行封装
<?
class SqlHelper{
private $mysqli;
//这里先写死,以后写死的东西用一个文件来配置
private static $host="localhost";
private static $user="root";
private static $pwd="root";
private static $db="test";
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);
}
//设置字符集
$this->mysqli->query("set names utf8");
}
//dql operate
function execute_dql($sql){
$res =$this->mysqli->query($sql) or die($this->mysqli->error);
return $res;
}
//dml operate
function execute_dml($sql){
$res =$this->mysqli->query($sql) or die($this->mysqli->error);
if(!$res){
return 0;//失败
}else{
if($this->mysqli->affected_rows>0){
return 1;//成功
}else{
return 2;//没有行到影响
}
}
}
}
?>
您可能感兴趣的文章:
linux下为php添加扩展库的方法
PHP添加CURL扩展库的二种方法
php mysqli扩展库之预处理操作的二个例子
php mysqli扩展库应用一例
[2]php mysqli实例 使用mysqli批量执行sql语句
来源: 互联网 发布时间: 2013-12-24
本节演示php中使用msyqli批量执行sql语句的例子。
1,mysqli查询,批量执行update\delete等语句
<?php
//mysqli批量执行sql语句
//批量执行dql
//使用mysqli::multi_query() 一次性添加3个用户
$mysqli =new MySQLi("localhost","root","root","test");
if($mysqli->connect_error){
die ("连接失败".$mysqli->connect_error);
}
//注意分号
$sqls="insert into user1 (name,password,email,age) values('AAA',md5('AAA'),'AAA@',25);";
$sqls.="insert into user1 (name,password,email,age) values('BBB',md5('BBB'),'BBB@',25);";
$sqls.="insert into user1 (name,password,email,age) values('CCC',md5('CCC'),'CCC@',25);";
//批量执行dml 可以混合使用delete insert update
//$sqls.="update user1 set age=15 where id=1;";
//$sqls.="delete from user1 where id=10";
$res=$mysqli->multi_query($sqls);
if(!$res){
echo "操作失败".$mysqli->error;
}else{
echo "OK";
}
?>
2、批量查询
<?php
//使用mysqli的mysqli::multi_query() 一次性查询表的机构和表中的内容
//edit:www.
//1、创建mysqli对象
$mysqli=new MySQLi("localhost","root","root","test");
if($mysqli->connect_error){
die("连接失败".$mysqli->connect_error);
}
//2、批量查询语句
$sqls="select *from user1;";
$sqls.="desc user1";
//3、处理结果
//如果成功,至少有一个结果集
if($res=$mysqli->multi_query($sqls)){
do{
//取出第一个结果集
$res=$mysqli->store_result();
while($row=$res->fetch_row()){
foreach($row as $val){
echo '--'.$val;
}
echo '<br/>';
}
//及时释放内存
$res->free();
//判断是否还有结果集
if($mysqli->more_results()){
echo "********新的结果集***************<br/>";
}else{
break;
}
}while($mysqli->next_result());
}
//4、关闭资源
$mysqli->close();
?>
[3]php 短信接口的示例代码(入门)
来源: 互联网 发布时间: 2013-12-24
php 短信接口文件:
<?
/*--------------------------------
功能: PHP HTTP接口 发送短信
修改日期: 2013-09-08
说明: http://www./tx/?uid=用户账号&pwd=MD5位32密码&mobile=号码&content=内容
状态:
100 发送成功
101 验证失败
102 短信不足
103 操作失败
104 非法字符
105 内容过多
106 号码过多
107 频率过快
108 号码内容空
109 账号冻结
110 禁止频繁单条发送
111 系统暂定发送
112 号码不正确
120 系统升级
--------------------------------*/
$uid = '9999'; //用户账号
$pwd = '9999'; //密码
$mobile = '13912341234,13312341234,13512341234,02122334444'; //号码
$content = 'PHP HTTP接口'; //内容
//即时发送
$res = sendSMS($uid,$pwd,$mobile,$content);
echo $res;
//定时发送
/*
$time = '2010-05-27 12:11';
$res = sendSMS($uid,$pwd,$mobile,$content,$time);
echo $res;
*/
function sendSMS($uid,$pwd,$mobile,$content,$time='',$mid='')
{
$http = 'http://www./tx/';
$data = array
(
'uid'=>$uid, //用户账号
'pwd'=>strtolower(md5($pwd)), //MD5位32密码
'mobile'=>$mobile, //号码
'content'=>$content, //内容
'time'=>$time, //定时发送
'mid'=>$mid //子扩展号
);
$re= postSMS($http,$data); //POST方式提交
if( trim($re) == '100' )
{
return "发送成功!";
}
else
{
return "发送失败! 状态:".$re;
}
}
function postSMS($url,$data='')
{
$row = parse_url(/blog_article/$url/index.html);
$host = $row['host'];
$port = $row['port'] ? $row['port']:80;
$file = $row['path'];
while (list($k,$v) = each($data))
{
$post .= rawurlencode($k)."=".rawurlencode($v)."&"; //转URL标准码
}
$post = substr( $post , 0 , -1 );
$len = strlen($post);
$fp = @fsockopen( $host ,$port, $errno, $errstr, 10);
if (!$fp) {
return "$errstr ($errno)\n";
} else {
$receive = '';
$out = "POST $file HTTP/1.1\r\n";
$out .= "Host: $host\r\n";
$out .= "Content-type: application/x-www-form-urlencoded\r\n";
$out .= "Connection: Close\r\n";
$out .= "Content-Length: $len\r\n\r\n";
$out .= $post;
fwrite($fp, $out);
while (!feof($fp)) {
$receive .= fgets($fp, 128);
}
fclose($fp);
$receive = explode("\r\n\r\n",$receive);
unset($receive[0]);
return implode("",$receive);
}
}
?>
猜你喜欢:php短信发送函数 php短信接口函数的实例代码最新技术文章: