您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

一段查询代码多处使用可以吗解决办法

2024/11/28 3:51:22发布23次查看
一段查询代码多处使用可以吗
$bliang='这是个变量'
$sql=select * from a表 where cart like '%.$bliang.%'
$query=mysql_query($sql);
while($row=mysql_fetch_array($query)){
…………
}
上边是查询代码   然后我使用它时  只需要这样  比如 $row[$bliang='分类2']   这样就可以分类2下的内容
$row[$bliang='分类3'] 就显示分类3下的内容
这样的需求  要怎么样实现呢
------解决方案--------------------
写成函数啊,我也是刚学php,我给你一个我自己做的类参考下吧,所有数据库操作都在这里了
dsn, $this->user, $this->pass);
  $dbh->query($this->names);
  return $dbh->query($sql);
  $dbh = null;
  }
  catch(exception $e) {
  echo 'error: ' . $e->getmessage();
  }
  }
  //操作单条数据(更新/删除/插入),无返回结果
  public function sql_one($sql) {
  try {
  $dbh = new pdo($this->dsn, $this->user, $this->pass);
  $dbh->exec($this->names);
  $dbh->exec($sql);
  $dbh = null;
  }
  catch(exception $e) {
  echo 'error: ' . $e->getmessage();
  }
  }
  //操作多条数据(更新/删除),无返回结果
  public function sql_more($sql, $str) {
  try {
  $dbh = new pdo($this->dsn, $this->user, $this->pass);
  $dbh->exec($this->names);
  foreach ($str as $arrs) {
  $dbh->exec($sql . $arrs);
  }
  $dbh = null;
  }
  catch(exception $e) {
  echo 'error: ' . $e->getmessage();
  }
  }
//参数化查询数据库返回结果(单条)
  public function cs_sql_select($sql,$str) {
  try {
  $dbh = new pdo($this->dsn, $this->user, $this->pass);
  $dbh->setattribute(pdo::attr_emulate_prepares, false);
  $dbh->query($this->names);
  $stmt = $dbh->prepare($sql);
  $stmt->bindvalue(:key, $str ,pdo::param_int);
  $stmt->execute();
  return $stmt;
  $dbh = null;
  }
  catch(exception $e) {
  echo 'error: ' . $e->getmessage();
  }
  }  
  //参数化查询操作多条数据(删除/更新),无返回结果
  public function cs_sql_more($sql, $str) {
  try {
  $dbh = new pdo($this->dsn, $this->user, $this->pass);
  $dbh->setattribute(pdo::attr_emulate_prepares, false);
  $dbh->exec($this->names);
  foreach ($str as $arrs) {
  $stmt = $dbh->prepare($sql);
  $stmt->bindvalue(:key, $arrs,pdo::param_int);
  $stmt->execute();
  }
  $dbh = null;
  }
  catch(exception $e) {
  echo 'error: ' . $e->getmessage();
  }
  }
  //参数化查询操作单条数据(删除/更新),无返回结果
  public function cs_sql_one($sql, $str) {
  try {
  $dbh = new pdo($this->dsn, $this->user, $this->pass);
  $dbh->setattribute(pdo::attr_emulate_prepares, false);
  $dbh->exec($this->names);
  $stmt = $dbh->prepare($sql);
  $stmt->bindvalue(:key, $str,pdo::param_int);
  $stmt->execute();
  $dbh = null;
  }
  catch(exception $e) {
  echo 'error: ' . $e->getmessage();
  }
  }
////////
}
?>

该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product