PHP SQL update 写法
请教一下,各位前辈,PHP面向对象怎么写语句呢?这个是添加语句,可以正常实现的functionfn_insert($table,$FieldName,$value){$t...
请教一下,各位前辈,PHP 面向对象怎么写 语句呢?
这个是添加语句,可以正常实现的
function fn_insert($table,$FieldName,$value)
{
$this->query("insert into $table($FieldName) value($value)");
}
$db->fn_insert('weblink','WebName,Website,Introduction',"'XXX','YYY','ZZZ'");
可是update语句应该怎么写呢?
function fn_update($table,$FieldName2,$value2,$FieldName1,$value1)
{
$this->query("update $table set $FieldName2=$value2 where $FieldName1=$value1");
}
$db->fn_update('weblink','Introduction','2222','link_id',34);
这样写是可以正常实现的,但是只能改其中 Introduction这一个字段的,现在想改三个字段怎么弄呢?下面这个怎么 写成 上面那样子呢?
update weblink set weblink=“abc”,WebName="def",Introduction="ghk" where link_id=29 展开
这个是添加语句,可以正常实现的
function fn_insert($table,$FieldName,$value)
{
$this->query("insert into $table($FieldName) value($value)");
}
$db->fn_insert('weblink','WebName,Website,Introduction',"'XXX','YYY','ZZZ'");
可是update语句应该怎么写呢?
function fn_update($table,$FieldName2,$value2,$FieldName1,$value1)
{
$this->query("update $table set $FieldName2=$value2 where $FieldName1=$value1");
}
$db->fn_update('weblink','Introduction','2222','link_id',34);
这样写是可以正常实现的,但是只能改其中 Introduction这一个字段的,现在想改三个字段怎么弄呢?下面这个怎么 写成 上面那样子呢?
update weblink set weblink=“abc”,WebName="def",Introduction="ghk" where link_id=29 展开
3个回答
展开全部
类页面
<?php
include_once (dirname(__FILE__)."\ConnDB.php");
class SqlHelper{
private $_arr;
public $tableName;
public function dataBind($key,$value)
{
$this->_arr[$key]=$value;
}
public function validate()
{
if(!isset($this->tableName))
{
die("表名不能为空!");
}
if(!isset($this->_arr))
{
die("值与列名未绑定");
}
}
public function update($condition){
foreach($this->_arr as $key => $value){
$s .= "$key = $value,";
}
$ss = substr($s,0,-1);
$sql = "update $this->tableName set $ss where $condition";
$query = mysql_query($sql);
echo $sql;
if($query)
{
echo "数据修改成功";
}
else
{
echo "数据修改失败";
}
}
public function insert()
{
$this->validate();
$keys = "";
$values = "";
foreach($this->_arr as $key=>$value)
{
$keys.="`$key`,";
if($value=="")
{
$values.="null,";
}
else
{
$values.="'$value',";
}
}
$keys = substr($keys,0,strlen($keys)-1);
$values = substr($values,0,strlen($values)-1);
$sql = "insert into `$this->tableName`($keys)values($values)";
$query = mysql_query($sql);
if($query)
{
echo "ok";
}
else
{
echo "no";
}
//echo mysql_error();
//return $query;
}
public function Delete($condition)
{
if(!isset($this->tableName))
{
die("表名不能为空!");
}
$sql="delete from $this->tableName where $condition";
$query=mysql_query($sql);
if($query)
{
echo "删除成功!";
}
else
{
echo mysql_error();
}
}
function select($sqls)//读取出符合where子句的所有信息
{
$sql = "select * from `$this->tableName` where $sqls";
$query = mysql_query($sql);
/*
$result = array();
while($rs = mysql_fetch_array($query))
{
$result[]=$rs;
}
return $result; */
$num = mysql_num_rows($query);
return $num;
}
function selectNew($sqls,$n)//读取出符合where子句的最新的n条信息
{
$sql = "select * from `$this->tableName`
where $sqls
order by `id` desc
limit 0,$n";
$query = mysql_query($sql);
$result = array();
while($rs = mysql_fetch_array($query))
{
$result[]=$rs;
}
return $result;
}
}
?>
实例化页面
$inserts = new SqlHelper();
$inserts->tableName="user";
$inserts->dataBind("name","bbbb");
$inserts->dataBind("address","");
$inserts->insert();
$inserts = new SqlHelper();
$inserts->tableName="user";
$inserts->dataBind("name","33333");
$inserts->dataBind("address","44444");
$inserts->update("id = 13");
$inserts=new SqlHelper();
$inserts->tableName="user";
$inserts->Delete("id=4");
这是我以前写的类,你看看!
<?php
include_once (dirname(__FILE__)."\ConnDB.php");
class SqlHelper{
private $_arr;
public $tableName;
public function dataBind($key,$value)
{
$this->_arr[$key]=$value;
}
public function validate()
{
if(!isset($this->tableName))
{
die("表名不能为空!");
}
if(!isset($this->_arr))
{
die("值与列名未绑定");
}
}
public function update($condition){
foreach($this->_arr as $key => $value){
$s .= "$key = $value,";
}
$ss = substr($s,0,-1);
$sql = "update $this->tableName set $ss where $condition";
$query = mysql_query($sql);
echo $sql;
if($query)
{
echo "数据修改成功";
}
else
{
echo "数据修改失败";
}
}
public function insert()
{
$this->validate();
$keys = "";
$values = "";
foreach($this->_arr as $key=>$value)
{
$keys.="`$key`,";
if($value=="")
{
$values.="null,";
}
else
{
$values.="'$value',";
}
}
$keys = substr($keys,0,strlen($keys)-1);
$values = substr($values,0,strlen($values)-1);
$sql = "insert into `$this->tableName`($keys)values($values)";
$query = mysql_query($sql);
if($query)
{
echo "ok";
}
else
{
echo "no";
}
//echo mysql_error();
//return $query;
}
public function Delete($condition)
{
if(!isset($this->tableName))
{
die("表名不能为空!");
}
$sql="delete from $this->tableName where $condition";
$query=mysql_query($sql);
if($query)
{
echo "删除成功!";
}
else
{
echo mysql_error();
}
}
function select($sqls)//读取出符合where子句的所有信息
{
$sql = "select * from `$this->tableName` where $sqls";
$query = mysql_query($sql);
/*
$result = array();
while($rs = mysql_fetch_array($query))
{
$result[]=$rs;
}
return $result; */
$num = mysql_num_rows($query);
return $num;
}
function selectNew($sqls,$n)//读取出符合where子句的最新的n条信息
{
$sql = "select * from `$this->tableName`
where $sqls
order by `id` desc
limit 0,$n";
$query = mysql_query($sql);
$result = array();
while($rs = mysql_fetch_array($query))
{
$result[]=$rs;
}
return $result;
}
}
?>
实例化页面
$inserts = new SqlHelper();
$inserts->tableName="user";
$inserts->dataBind("name","bbbb");
$inserts->dataBind("address","");
$inserts->insert();
$inserts = new SqlHelper();
$inserts->tableName="user";
$inserts->dataBind("name","33333");
$inserts->dataBind("address","44444");
$inserts->update("id = 13");
$inserts=new SqlHelper();
$inserts->tableName="user";
$inserts->Delete("id=4");
这是我以前写的类,你看看!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
update 如果要是用数组来插入的话 可以用FOREACH 循环
$array 为POST 或者GET的数组 当然字段要都是表里面的字段
function update($array)
{
if($this->where == '')
{
$where = '';
}else{
$where = ' where '.$this->where;
}
if(empty($array))
{
echo "不能插入空数组";exit;
}
$str = '';
foreach($array as $key => $val)
{
$str .= '`'.$key.'`'.'='.'\''.$val.'\''.',';
}
$str = substr($str,0,-1);
$sql ="update `".$this->table."` set ".$str.$where." ";
$this->query($sql);
}
$array 为POST 或者GET的数组 当然字段要都是表里面的字段
function update($array)
{
if($this->where == '')
{
$where = '';
}else{
$where = ' where '.$this->where;
}
if(empty($array))
{
echo "不能插入空数组";exit;
}
$str = '';
foreach($array as $key => $val)
{
$str .= '`'.$key.'`'.'='.'\''.$val.'\''.',';
}
$str = substr($str,0,-1);
$sql ="update `".$this->table."` set ".$str.$where." ";
$this->query($sql);
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
function fn_update($table,$FieldName2,$value2,$FieldName1,$value1)
{
$this->query("update `$table` set $FieldName2 where $FieldName1");
}
$db->fn_update('weblink','`weblink` = \'abc\', `WebName` = \'def\',`Introduction` = \'ghk\'','`link_id` = 29');
{
$this->query("update `$table` set $FieldName2 where $FieldName1");
}
$db->fn_update('weblink','`weblink` = \'abc\', `WebName` = \'def\',`Introduction` = \'ghk\'','`link_id` = 29');
追问
超久不见你了,以为你失踪了呢,再请教一下啊:
function fn_select($table){ //搜查功能
$this->query("select * from $table);
}
$db->fn_select(‘weblink’); //在这个地方怎么添加,where ~ order by ~语句呢?
如果放到上面的function里面吧,不是每个地方都需要啊。
追答
function fn_select($table='',$duan='',$where='',$xu='',$num=1){
if($duan==''){$duan= ' * ';}
$sql="slect $duan from $table";
if($where!=''){$sql.=" where $where";}
if($xu!=''){$sql.=" order by $xu";}
$sql.=" limit $num";
$this->query($sql);
}
$db->fn_select(‘weblink’);//无条件返回weblink所有字段
$db->fn_select('weblink','`id`');//返回weblink的id字段
$db->fn_select('weblink','`id`','`id` = 1');//返回weblink的id字段等于1的id字段
$db->fn_select('weblink','`id`','`id` fn_select('weblink','`id`','`id` fn_select('weblink','`id`','`id` < 20','`id` DESC',10);//返回weblink的id字段小于20的id字段,返回10条记录,降序排列。
有时候,给函数的参数给定默认值,是个好习惯
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询