: This article mainly introduces some summary of php connection to the database. if you are interested in the PHP Tutorial, please refer to it. In php project development, the mvc framework is generally used for development, and the interaction with the database is generally carried out in the model. The following describes how to process php interaction with databases:
1. direct processing:
- $ Link = mysql_connect ("host name", "user name", "password ");
- Mysql_select_db ("database name", $ link );
- $ Result = mysql_query ("SELECT * FROM table name", $ link );
- While ($ row = mysql_fetch_assoc ($ result )){
- Foreach ($ row as $ v ){
- Echo $ v ['name'];
- }
- }
2. processing in the CI and other frameworks:
Configure the database information in database. php of config (refer to the existing configuration format );
The following databases can be accessed in the CI framework:
- $ This-> load-> database ('database name ');
- $ Query = $ this-> db-> get ('Table name ');
- Foreach ($ query-> result () as $ row ){
- Echo $ row-> name;
- }
3. PDO mode;
Common interfaces are also frequently used in the framework and need to expand the PDO module;
$ Db_conn = new PDO ("data source", "username", "password ");
$ This-> conn = $ db_conn;
$ Stmt = $ this-> conn-> prepare ($ SQL );
$ Stmt-> execute ($ data );
$ Res = $ stmt-> fetch ();
In actual development, you can write your own database access interface form based on the above methods to complete more convenient functions.
If you have any supplements or corrections, you may want to reply to them for further discussion.
The above introduces some summary of php connection to the database, including some content, hope to be helpful to friends who are interested in PHP tutorials.