A query code can be used in many ways to solve the problem

Source: Internet
Author: User
Tags dsn
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 ();
}
}


////////
}
?>
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.