日期:2014-05-16 浏览次数:20557 次
$bliang='这是个变量'
$sql=SELECT * FROM A表 WHERE cart LIKE '%.$bliang.%'
$query=mysql_query($sql);
while($row=mysql_fetch_array($query)){
…………
}
<?php
//
------解决方案--------------------
PHP version 5.3
// 数据库操作类 Order by phuai007
// Date 2014/2
class my_sql {
public $dsn = 'mysql:host=localhost;dbname=lif2';//host为数据库连接地址,dbname为数据库名
public $user = 'root'; //数据库连接用户名
public $pass = '123456'; //对应的密码
public $names = 'SET NAMES UTF8'; //数据库查询编码
//查询数据库返回结果
public function sql_select($sql) {
try {
$dbh = new PDO($this->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