简单mysql封装,写脚本用,好使

<?php
	

	$result = getall('SELECT * FROM yoshop_user');
	// print_r($result);
	foreach($result as $k=>$v)
	{
		$re = getone("select * from yoshop_user_address where user_id='{$v['user_id']}'");
		if(empty($re))
		{
			$sql = "INSERT INTO yoshop_user_address (province_id, city_id, region_id,user_id,create_time,update_time,wxapp_id)
			VALUES (1,2,3,'{$v['user_id']}', ".time().", ".time().",10001)";
			insert($sql);
		}
		// var_dump($re);die;
	}
	echo 'finished';
	function insert($sql)
	{
		$con=mysqli_connect("localhost","root","root","yoshop");
		 
		// 创建连接
		
		// 检测连接
		if (mysqli_connect_errno($con)) 
		{ 
			echo "连接 MySQL 失败: " . mysqli_connect_error(); 
		} 
		 
	
		if (mysqli_query($con, $sql)) {
			echo "新记录插入成功\n";
		} else {
			echo "Error: " . $sql . "<br>" . mysqli_error($con)."\n";
		}
		$con->close();
	}
	function getone($sql)
	{
		$con=mysqli_connect("localhost","root","root","yoshop");
		if (mysqli_connect_errno($con)) 
		{ 
			echo "连接 MySQL 失败: " . mysqli_connect_error(); 
		} 
		mysqli_set_charset($con,"utf8");
		// 执行查询
		$result= mysqli_query($con,$sql);
	
		$row = mysqli_fetch_array($result);

		
		mysqli_close($con);		
		return $row;
	}
	function getall($sql)
	{
		$con=mysqli_connect("localhost","root","root","yoshop");
		if (mysqli_connect_errno($con)) 
		{ 
			echo "连接 MySQL 失败: " . mysqli_connect_error(); 
		} 
		mysqli_set_charset($con,"utf8");
		// 执行查询
		$result= mysqli_query($con,$sql);
		$arr = array();
		while($row = mysqli_fetch_array($result))
		  {
			$arr[] = 	$row;
		
		  }
		
		mysqli_close($con);		
		return $arr;
	}

?>
© 版权声明
THE END
喜欢就支持一下吧
点赞10 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片