1.获取数据库中所有数据表的条数累加去判断(多项目时)
2.选取新闻表和栏目表,获取总条数,与下次作比较判断(单项目时)
建议选取第一种:
3.连接数据库代码:
function databasecont($db_host,$db_name,$port,$user,$pass,$id){ // $db_config = config('database'); $conparas = [ // 数据库类型 'type' => 'mysql', // 服务器地址 'hostname' => $db_host, // 数据库名 'database' => $db_name, // 用户名 'username' => $user, // 密码 'password' => $pass, // 数据库表前缀 'prefix' => 'tp_', // 端口 'hostport' => $port, //断线重连 'break_reconnect' => true, ]; $database_config = Config::get('database'); //新增数据库参数配置 $database_config['connections']['db_name_tx_def']= $conparas; //设置database参数为 修改过的database配置参数 $config = Config::set($database_config, 'database'); // var_dump($config); return Db::connect('db_name_tx_def',true); }
调用以及判断更新:
$link=databasecont($daseinfo['baseurl'],$daseinfo['basename'],$daseinfo['baseport'],$daseinfo['basetitle'],$daseinfo['basepass'],$daseinfo['id']); $table_info =$link->query("show tables from ".$daseinfo['basename']); foreach ($table_info as $key => $value) { $atable[]=$value['Tables_in_'.$daseinfo['basename']]; } $coun=0; foreach ($atable as $key => $value) { $sql_count=$link->query("select count(*) as up from ".$value); $co[]= $sql_count[0]['up']; } $count=array_sum($co); if($count!=$daseinfo['upcount']){ $tj=$count-$daseinfo['upcount']; echo '<h3 style="color:red;font-size:16px;">新闻已更新'.$tj.'条数据!</h3>'; $daseinfo=Db::name('website')->where(['id'=>$daseinfo['id']])->update(['upcount'=>$count]); }else{ echo '新闻无更新,原数据'.$daseinfo['upcount'].'条数据!'; }
4.实现的效果:
标签:count,name,database,为例,db,更新,tp6,config,daseinfo From: https://www.cnblogs.com/mo3408/p/17073478.html