This article to the PHP beginners in detail about the PHP connection database instance code, here is mainly about the entry-level MySQL connection code to the Advanced Packaging database connection class, I hope this article to all the help of friends.
There are two ways to connect a MySQL database :
(1) using PHP database functions to connect
This is one of the most common ways.
Here we use four database functions mainly:
Mysql_connect () establishes a connection to the MySQL server.
mysql_select_db (): Select a database from the MySQL server for future data query Operations query processing.
mysql_query (): Send a query string to help MySQL do the related processing or execution.
Mysql_fetch_row (): Used to move the result line of the query results into an array variable. The index of the array is a number
Index, the first index value is 0.
(2) Connect via ODBC
PHP uses ODBC to connect MySQL database with four main functions:
Odbc_connect (): Used to establish a connection with an ODBC data source.
ODBC_DO (): Used to execute a database query after a connection has been established.
Odbc_result (): Used to get the value of a field in the current record row.
Odbc_fetch_row (): Used to save query results to an array, with each array element corresponding to a record.
Let's take a look at PHP's database function connection Method Example
Connect to a MySQL database
Before you can access and process data in a database, you must create a connection to the database.
In PHP, this task is done through the mysql_connect () function.
Grammar
Mysql_connect (Servername,username,password); parameter description
ServerName is optional. Specifies the server to connect to. The default is "localhost:3306".
Username is optional. Specifies the user name to use for the login. The default value is the name of the user who owns the server process.
Password is optional. Specifies the password used to log in. The default is "".
The code is as follows |
Copy Code |
$con = mysql_connect ("localhost", "root", ""); if (! $con) { Die (' Could not connect: '. Mysql_error ()); } Mysql_close ($con); ?> |
Object-oriented mysqli (detailed tutorial)
The code is as follows |
Copy Code |
$mysqli = new mysqli (' localhost ', ' root ', ' ', ' volunteer '); if (Mysqli_connect_errno ()) { Die (' Unable to connect! '). Mysqli_connect_error (); } ?>
|
PDO connection MySQL (detailed tutorial)
The code is as follows |
Copy Code |
$db = new PDO (' Mysql:host=localhost;dbname=test ', ' root ', '); try { foreach ($db->query (' select * from user ') as $row) { Print_r ($row); } $DB = null; Close the database } catch (Pdoexception $e) { echo $e->getmessage (); } ?>
|
Then we can also use ODBC to connect to the database
The code is as follows |
Copy Code |
Require_once './adodb5/adodb.inc.php '; $conn = &adonewconnection (' mysql '); $conn->connect (' localhost ', ' root ', ' ', ' test '); $conn->execute ("Set names UTF8"); $res = $conn->execute ("SELECT * from user"); if (! $res) { echo $conn->errormsg (); }else{ Var_dump ($res); } ?>
|
MySQL Data connection class
The code is as follows |
Copy Code |
//------------------------------------------------------------------------------------------ ※database () constructor, database initial parameters ※select () query ※getrows () returns the total number of records for the query ※insert () Inserting records ※update () Update ※delete () Delete ※halt () Interrupt and display error message */ //------------------------------------------------------------------------------------------ Define ("DatabaseType", "1"); Define database type: 1 mysql;2 for SQL Server;3 to oracle;4 for ODBC Define ("SERVER", "localhost"); Host name or IP address of the database server Define ("DATABASE", "dbName"); The name of the database to connect to Define ("USER", "tableName"); User name used to connect to the database Define ("PASSWORD", "paswd"); The password used to connect to the database
Class Database { var $dbLink; Connection handle var $result; Query handle var $insId; Insert () successfully returns the value of the Auto_increment column var $rows; Returns an array of data var $numRows; Number of returned data var $dbHost, $dbUser, $userPassword, $database; var $dbType = DatabaseType; var $msgFlag = "Yes"; Yes:show the MYSQL message; No:die by show "halted." function Database ($dbHost = SERVER, $dbUser = USER, $userPassword = PASSWORD, $database = Database) { Switch ($this->dbtype) { Case 1: $this->dblink = @mysql_pconnect ($dbHost, $dbUser, $userPassword); Or Die ("Can ' t Connect to Remote host!"); @mysql_select_db ($database, $this->dblink); Or Die ("Can ' t Connect to Remote host!"); Break Case 2: Break } return true; } /* Sql:select () returns false for no results */ function Select ($table, $columns, $condition = 1) { $sql = "Select $columns from $table where $condition"; $this->result = @mysql_query ($sql, $this->dblink); unset ($this->rows); if ($this->result) { $i = 0; if (! ( $this->rows = Array ("$i" = = @mysql_fetch_array ($this->result))) return false; if ($this->numrows = @mysql_num_rows ($this->result)) = = = 0) return false; while ($tempRows = @mysql_fetch_array ($this->result)) { Array_push ($this->rows, $tempRows); } } else { $this->halt ($sql); return false; } return true; } /* Sql:getrows () returns the total number of records for the query */ function GetRows ($table, $condition = 1) { $sql = "SELECT count (1) as Count from $table where $condition"; $this->result = @mysql_query ($sql, $this->dblink); if ($this->result) { $temp = @mysql_fetch_array ($this->result); $this->numrows = $temp [Count]; } else { $this->halt ($sql); return false; } return $this->numrows; } /* Sql:insert () */ function Insert ($table, $columns, $values) { $sql = "INSERT into $table ($columns) VALUES ($values)"; $this->result = @mysql_query ($sql, $this->dblink); if ($this->result) $this->insid = @mysql_insert_id ($this->dblink); else { $this->halt ($sql); return false; } return true; } /* Sql:update () */ function Update ($table, $setings, $condition) { $sql = "Update $table set $setings where $condition"; $this->result = @mysql_query ($sql, $this->dblink); if ($this->result) $this->numrows = @mysql_affected_rows ($this->result); else { $this->halt ($sql); return false; } return true; } /* Sql:delete */ function Delete ($table, $condition) { $sql = "Delete from $table where $condition"; $this->result = @mysql_query ($sql, $this->dblink); if ($this->result) $this->numrows = @mysql_affected_rows ($this->result); else { $this->halt ($sql); return false; } return true; } /* Halt (): Error message */ function Halt ($msg) { if ($this->msgflag = = = "Yes") { printf ("Database Query Error: %s n ", $msg); printf ("MySql Error: %s N ", Mysql_error ()); }else echo " ";//Customize an error prompt file return false; } } Switch ($db->dbtype) { Case 1: @mysql_close (); Break Case 2: Break } $db = new Database (); ?> |
Friendly Tips
If the connection MySQL database is garbled, we can add mysql_query ("Set names UTF8") before connecting to the database query. If you're GBK, you're using GBK coding.
http://www.bkjia.com/PHPjc/630682.html www.bkjia.com true http://www.bkjia.com/PHPjc/630682.html techarticle This article to give you PHP beginners detailed information about the PHP connection database instance code, here is mainly about the entry-level MySQL connection code to the Advanced Packaging database connection class, I hope this article ...