<?php
//示例是数据匹配实例,两张表通过关联字段email匹配mobile
header("content-type:text/html;charset=utf-8");
set_time_limit(0);
$con = mysqli_connect("127.0.0.1","root",'','test');
if(mysqli_connect_error($con)){
die("数据库连接错误:".mysqli_connect_error());
}
//设置查询编码
$con->query("set names utf8");
$sql = "SELECT * FROM `shuju` WHERE mobile<1 ";
//执行sql
$result = $con->query($sql);
//查询所有条数,循环取出条数
while ($row=$result->fetch_array()) {
$data[] = $row;
}
//初始化参数
$u = $success=$error = 0 ;
//循环第一张表的查询结果
foreach($data as $v){
$email = trim($v['email']);
//查询关联条件,此处字段在数据库里应设索引
$map = "email = '$v['email']";
//根据关联条件查询第二张表
$sql2 = "SELECT * FROM `new` WHERE $map";
$r = mysqli_query($con,$sql2);
//从数据库中取出单条记录
$ree = mysqli_fetch_assoc($r);
if(!empty($ree)){
$success++;
//模糊匹配
// $where = 'email like "%'.$email.'%"';
//==匹配
$where = "email = '$email'";
$sql3 = "UPDATE `shuju` set `mobile`=".$ree['mobile']."WHERE $where";
$ba = mysqli_query($con,$sql3);
if($b==true){
$su++;
}
}else{
$error++;
}
}
echo '成功数:'.$success.'<br/>';
echo '更新成功数:'.$su.'<br/>';
echo '找不到匹配的:'.$error.'条';