Can I use multiple query codes? $ bliang = 'this is A variable' $ SQL = SELECT & nbsp; * & nbsp; FROM & nbsp; table A & nbsp; WHERE & nbsp; cart & nbsp; LIKE & nbsp; '%. $ bliang. % '$ query = mysql_query ($ SQL); can I use multiple pieces of query code?
$ Bliang = 'This is a variable'
$ SQL = SELECT * FROM table A WHERE cart LIKE '%. $ bliang. %'
$ Query = mysql_query ($ SQL );
While ($ row = mysql_fetch_array ($ query )){
............
}
The above is the query code, and when I use it, I only need to use it like this, for example, $ row [$ bliang = 'category 2'] to classify the content under 2.
$ Row [$ bliang = 'Category 3'] displays the content under Category 3.
How can this requirement be achieved?
------ Solution --------------------
I just learned php, and I will give you a reference for my own classes. all database operations are here.
//
------ Solution --------------------
PHP version 5.3
// Database operation Order by phuai007
// Date 2014/2
Class my_ SQL {
Public $ dsn = 'MySQL: host = localhost; dbname = lif2 '; // host indicates the database connection address, and dbname indicates the database name.
Public $ user = 'root'; // database connection username
Public $ pass = '000000'; // password
Public $ names = 'set NAMES utf8'; // database query encoding
// Query the database return 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 (update/delete/insert) with no returned results
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 ();
}
}
// Operate on multiple data records (update/delete) with no returned results
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 ();
}
}
// Results returned by the parameterized Query Database (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: PARAM_INT );
$ Stmt-> execute ();
Return $ stmt;
$ Dbh = null;
}
Catch (Exception $ e ){
Echo 'Error: '. $ e-> getMessage ();
}
}
// Parameters query multiple data records (delete/update) with no returned 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: PARAM_INT );
$ Stmt-> execute ();
}
$ Dbh = null;
}
Catch (Exception $ e ){
Echo 'Error: '. $ e-> getMessage ();
}
}
// Parameterized query operation (delete/update) with no returned 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: PARAM_INT );
$ Stmt-> execute ();
$ Dbh = null;
}
Catch (Exception $ e ){
Echo 'Error: '. $ e-> getMessage ();
}
}
////////
}
?>