- mysql_connect ("localhost", "root", "1981427")//Connect to server on localhost, user name is root
- ?>
Copy Code
Connect and select a database
- @mysql_select_db ("test")//Select Database MyDB
- ?>
Copy Code
This makes it possible to connect to the database.
Run code appears: Call to undefined function ' mysql_connect () ' ... Failure this prompt function undefined is said to have no mysql_connect () functions,
Workaround: Copy the Php_mysql.dll and Libmysql.dll files to the C:winntsystem32 (I missed the Libmysql.dll) and find the php.ini, and remove the ";" from the front. Restart Server example 1,conn.php file code:
- $conn = @mysql_connect ("localhost", "root", "root") or Die ("Database link error");
- mysql_select_db ("Data", $conn);//data is the database name
- mysql_query ("Set names ' GBK '"); Use GBK Chinese code;
- ?>
Copy Code
index.php File Code:
- Include ("conn.php");//introduction of conn.php File
- $sql = "SELECT * from ' table ' ORDER by id DESC";
- $query =mysql_query ($sql);
- while ($row =mysql_fetch_array ($query)) {
- ?>
Copy Code
Display data content:
- }
- ?>
Copy Code
PHP connection MySQL Database
Method One: Common method (Process oriented)
First, database user information:
-
Generate a connection
- $db _connect=mysql_connect ($dbhost, $username, $userpass) or Die ("unable-to-connect to the mysql!");
- Select a database to operate on
- mysql_select_db ($dbdatabase, $db _connect);
- Execute MySQL Statement
- $result =mysql_query ("Select Id,name from user");
-
Extracting data
- $row =mysql_fetch_row ($result);
Copy Code
Add: ① use @ (Error control operator) before functions such as mysql_connect (), mysql_select_db (), can ignore the error message generated by the system, and then we use Die () to customize the error message;
② extract data, in addition to the above mysql_fetch_row, there are common Mysql_fetch_assoc and mysql_fetch_array, the specific differences please check PHP manual;
③ for the return value of the mysql_query () function, if the executed statement has a return value (such as SELECT, Show, describe, and so on), the corresponding data is returned (on success) or FALSE (on failure), if the executed statement has no return value (such as Delete, Drop, Insert, update, and so on), returns True (on success) or FALSE (on failure).
Method Two: Php Object-oriented method to connect MySQL database
In fact, this method is very similar to the normal method, just replace the corresponding function with the object-oriented method, directly look at the code.
- $db =new mysqli ($dbhost, $username, $userpass, $dbdatabase);
- if (Mysqli_connect_error ()) {
- Echo ' could not connect to database. ';
- Exit
- }
- $result = $db->query ("Select Id,name from user");
- $row = $result->fetch_row ();
Copy Code
Here is the mysqli, meaning that the MySQL extension, either through a process-oriented approach or object-oriented way to interact with the database, the only difference is to call the function (object method) in a different way.
Method Three: Php PDO method to connect MySQL database
PDO is actually the abbreviation for PHP database objects, which is the PHP object in Chinese. It provides a unified way for PHP to interact with the database.
This is a popular way to connect a database at present. The advantage of this is that the basic operation of the database is the same as long as the data source is provided correctly. That is, the same piece of code can either interact with MySQL, interact with sqlite3, and, of course, interact with PostgreSQL, provided you have the right data source. Code to connect to MySQL:
-
$dsn = ' mysql:host= '. $dbhost. '; Dbname= '. $dbdatabase. '; '
- $DBH =new PDO ($DSN, $username, $userpass);
-
Sqlite3:
- $dsn = ' sqlite3: ' c:\sqlite\user.db ';
- $DBH =new PDO ($DSN);
-
PostgreSQL
- $dsn = ' pgsql:host= '. $dbhost. ' port=5432 dbname= '. $dbdatabase. ' user= '. $username. ' password= '. $userpass;
- $DBH =new PDO ($DSN);
-
Copy Code
After a successful connection with the database, the following only needs to fetch data from the database or insert the update data.
SQL code:
- $stmt = $dbh->query (' Select id,name from user ');
- $row = $stmt->fetch ();
Copy Code |