Can you use a query code in multiple places?
$bliang = ' This is a variable '
$sql =select * from a table WHERE cart like '%. $bliang.% '
$query =mysql_query ($sql);
while ($row =mysql_fetch_array ($query)) {
............
}
The top is the query code and then I use it just like this $row [$bliang = ' Category 2 '] so that you can classify the content under 2
$row [$bliang = ' Category 3 '] displays the content under Category 3
How does this need to be achieved?
------Solution--------------------
Write a function Ah, I also just learned PHP, I give you a I do the class reference, all database operations are here
//
------Solution--------------------
PHP version 5.3
Database Operations Class Order by phuai007
Date 2014/2
Class My_sql {
Public $dsn = ' mysql:host=localhost;dbname=lif2 ';//host is the database connection address, dbname is the database name
Public $user = ' root '; Database connection user Name
Public $pass = ' 123456 '; The corresponding password
Public $names = ' SET names UTF8 '; Database query encoding
Querying the database returns results
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 ();
}
}
Operation of single data (update/delete/insert), no results returned
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 ();
}
}
Operation of multiple data (update/delete), no results returned
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 ();
}
}
Parameterized Query Database returns results (single)
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::P aram_int);
$stmt->execute ();
return $stmt;
$DBH = null;
}
catch (Exception $e) {
Echo ' ERROR: '. $e->getmessage ();
}
}
Parameterized query operation multiple data (delete/update), no return results
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::P aram_int);
$stmt->execute ();
}
$DBH = null;
}
catch (Exception $e) {
Echo ' ERROR: '. $e->getmessage ();
}
}
Parameterized query Operation single data (delete/update), no return results
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::P aram_int);
$stmt->execute ();
$DBH = null;
}
catch (Exception $e) {
Echo ' ERROR: '. $e->getmessage ();
}
}
////////
}
?>