Home  >  Article  >  Backend Development  >  Some summary of php connecting to database

Some summary of php connecting to database

WBOY
WBOYOriginal
2016-08-08 09:27:011216browse

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 is a brief introduction to some PHP processing when interacting with the database:

1. Direct processing:

  1. $link = mysql_connect("hostname", "username", "password");
  2. mysql_select_db("database name", $link);
  3. $result = mysql_query("SELECT * FROM table name", $link);
  4. while ($row = mysql_fetch_assoc($result)) {
  5. foreach ( $row as $v) {
  6.                                                                                                                                          $row                                                                             $row Database information (can refer to the existing configuration format);
  7. The database can be accessed in the CI framework as follows:

$this->load->database('database name') ;
$query = $this->db->get('table name');

foreach ($query->result() as $row) {

echo $row->name; }

    3.PDO method;
  1. Commonly used interfaces are also often used in frameworks and need to extend the PDO module;

=

new

$stmt = $this-> conn
->prepare( $sql ); $stmt
->execute(
$data

);$res = $stmt ->fetch ();In actual development, we will write our own interface form for accessing the database based on the above method to complete more convenient functions. If you have any additions or corrections, please reply and communicate.

The above introduces some summary of PHP connection to the database, including aspects of the content. I hope it will be helpful to friends who are interested in PHP tutorials.
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn