Thanks for writing oop, It is a database connection Class & lt ;? Phpclass & nbsp; db {public & nbsp; $ conn; function & nbsp ;__ construct () {$ this-& gt; conn = mysqli_connect ('localhost', 'root ', I wrote the oop
Thanks. It's a database connection class.
Class db {
Public $ conn;
Function _ construct (){
$ This-> conn = mysqli_connect ('localhost', 'root', '123', 'air ');
// $ This-> conn-> query ('set NAMES utf8 ');
Mysqli_query ($ this-> conn, 'set NAMES utf8 ');
}
Public function delete ($ table, $ condition = ""){
If (empty ($ condition )){
$ This-> halt ('the condition for deletion not set ');
Return false;
}
$ SQL = "delete from". $ table. "where 1 = 1 and". $ condition;
If (! $ This-> conn-> query ($ SQL )){
Return false;
} Else {
Return true;
} // If
}
Public function getAll ($ SQL) {// output all Arrays
$ Rt = array ();
$ Q = $ this-> conn-> query ($ SQL );
$ This-> conn-> query ('set NAMES utf8 ');
While ($ r = $ q-> fetch_array (MYSQLI_USE_RESULT )){
$ Rt [] = $ r;
}
Return $ rt;
}
Public function _ destruct (){
Mysqli_close ($ this-> conn );
}
} // Db
?>
------ Solution --------------------
I feel that the connected database should not be detached from the data access layer, so if you want to access other databases, this class has to be modified, so we should propose it separately, in this way, you do not need to modify the data access layer in the later stage. You can directly change the configuration.
If (! $ This-> conn-> query ($ SQL )){
Return false;
} Else {
Return true;
}
It seems that you can also write a function separately.
------ Solution --------------------
First, the connection $ conn should be modified to private (encapsulation)
Second: organize SQL statements in the logic layer (according to the MVC design mode)
Third: the database operation class, generally write a dml (modify), dql (query) two functions. So your getAll should not be written in this class (I usually write code in this class like SqlHelper. class. php, which writes an execute_dml function and an execute_dql function. The constructor is used to connect to the database)
Haha !!! I also just learned PHP, cainiao. The above is my personal opinion. I don't know if it's right or not...
------ Solution --------------------
First of all, this OO design cannot be absolutely said to be so good or so good. The interface provided by a class should be determined based on requirements, the classification function is determined based on requirements, so no one else can judge it out of thin air.
For example, if you have an insert statement or a query (getAll) statement in this class, you cannot see the update statement, or you have not completed the update statement. However, if your application does not need to be updated at present or in the future, there is nothing wrong with it.
Let's talk about something in detail:
1. 'set NAMES utf8' does not need to be in getAll. Connect and run it once.
2. Mix $ this-> conn-> query and mysqli_query to make the query look better.
3. the interfaces of delete ($ table, $ condition = "") and getAll ($ SQL) are inconsistent. Using this class will be a headache.
4. Check for errors after mysqli_connect and $ this-> conn-> query
.....
X. The code is not fully pasted: $ this-> what is halt?