第一步:首先连接一个数据库的类
1.名字可以自己命名 比如这个名字就叫conncl.php
final class database
{
var $hostname_conn = "localhost";
var $database_conn = "hongtui";
var $username_conn = "root";
var $password_conn = "root";
var $charset = 'utf8';
function query($sql)
{
global $hostname_conn,$database_conn,$username_conn,$password_conn,$charset;
$conn = mysql_pconnect($this->hostname_conn, $this->username_conn, $this->password_conn) or trigger_error(mysql_error(),E_USER_ERROR);
mysql_select_db($this->database_conn, $conn);
$result=mysql_query($sql);
return $result;
}
function fetch_array($query)
{
return mysql_fetch_array($query,MYSQL_BOTH);
}
function num_rows($query)
{
return mysql_num_rows($query);
}
function Insert($sql)
{
$result = $this->query($sql);
if (!$result){
echo $this->error;
}
/*
while($arr =$this->fetch_array($result))
{
$arar[] = $arr;
}
*/
return $result;
}
function Execute($SQL) //直接执行SQL语句
{
if(empty($SQL))
{
$this->nErr=1;
$this->sErr="Execute:执行语句不能为空!";
return false;
}
$this->sSQL=$SQL;
if(!mysql_query($SQL))
{
$this->nErr=1;
$this->sErr="Execute:SQL语句:".$SQL."
MySql错误:".mysql_error();
return false;
}
return true;
}
function Search($sql)
{
//$arar = array();
$result = $this->query($sql);
//echo $sql;die;
if (!$result){
echo $this->error;
}
while($arr =$this->fetch_array($result))
{
$arar[] = $arr;
}
return $arar;
}
}
第二步:自己写一个类 里面存放自己的sql语句
1.首先引用一下上面的数据库类,然后给一个编码格式防止乱码代码如下
header("Content-type:text/html;charset=utf-8");
require_once("conncl.php");
class ProInfo
{
function selwxPlatform()
{
$db = new database();
$sql = "SELECT wechat_number,wechat_qq,wechat_phone,wechat_email,wechat_comment,wechat_platform FROM `ht_wechatnumber8` where wechat_platform!='' group by wechat_platform";
//echo $sql;exit;
$result = $db->Search($sql); //列举一个查询的语句
return $result;
}
function update_media($wechat_qq ,$wechat_phone,$wechat_email ,$wechat_comment,$wechat_platform)
{
$db = new database();
@mysql_query("set names utf8");
//列举一个修改的语句
$sql = "update ht_media set media_qq='$wechat_qq',media_mobile='$wechat_phone',media_email='$wechat_email',media_invoice='$wechat_comment' where media_name= '$wechat_platform'";
//echo $sql;exit;
$result = $db->Execute($sql);
return $result;
}
}
第三步:执行
1.所谓的最后一步也就是最重要的一步就是数据执行了
//报告运行时错误
error_reporting(E_ALL ^ E_DEPRECATED);
header("Content-type:text/html;charset=utf-8");
//引用一个数据处理的类
require_once("sel_hongtui.php");
//实例化一个类
$sel = new ProInfo();
$getnn = $sel->selwxPlatform();
//设置字符集
@mysql_query("set names utf8");
print_r($getnn);exit;
if(!empty($getnn)){
foreach($getnn as $k=>$v){
$wechat_qq = $wechat_phone = $wechat_email = $wechat_comment = $wechat_platform ='' ;
$wechat_qq = trim($v['wechat_qq']);
$wechat_phone = trim($v['wechat_phone']) ;
$wechat_email = trim($v['wechat_email']);
$wechat_comment = trim($v['wechat_comment']) ;
$wechat_platform = trim($v['wechat_platform']) ;
$update = $sel->update_media($wechat_qq ,$wechat_phone,$wechat_email ,$wechat_comment,$wechat_platform);
if($update)
echo 'sucess'.$k.'<br>';
else
echo 'failute'.'<br>';
//$i++;
}
}