Several common PHP database connection code examples. PHP connection to MYSQL database code? Php $ mysql_server_namelocalhost; change it to your mysql database server $ mysql_usernameroot; change it to your my
PHP connection to MYSQL database code
- <? Php
- $ Mysql_server_name = 'localhost ';
- // Change to your mysql database server
- $ Mysql_username = 'root ';
- // Change the username of your mysql database
- $ Mysql_password = '000000 ';
- // Change your mysql database password
- $ Mysql_database = 'mycounter ';
- // Change the name of your mysql database
- $ Conn = mysql_connect ($ mysql_server_name,
$ Mysql_username, $ mysql_password,
$ Mysql_database );
- $ SQL = 'create DATABASE mycounter
Default character set gbk COLLATE gbk_chinese_ci;
- ';
- Mysql_query ($ SQL );
- $ SQL = 'create TABLE 'counter'
('Id' INT (255) UNSIGNED NOT NULL
AUTO_INCREMENT, 'count' INT (255)
Unsigned not null default 0, PRIMARY KEY
('Id') TYPE = innodb ;';
- Mysql_select_db ($ mysql_database, $ conn );
- $ Result = mysql_query ($ SQL );
- // Echo $ SQL;
- Mysql_close ($ conn );
- Echo "Hello! The database mycounter has been successfully created! ";
- ?>
PHP connection to database: PHP connection to ACCESS database code method
- < ?
- $conn = new com("ADODB.Connection");
- $connstr = "DRIVER={Microsoft
Access Driver (*.mdb)};
DBQ=". realpath("data/db.mdb");
- $conn->Open($connstr);
- $rs = new com("ADODB.RecordSet");
- $rs->Open("select *
from szd_t",$conn,1,1);
- while(! $rs->eof) {
- $f = $rs->Fields(1);
- echo $f->value;
- $rs->MoveNext();
- }
- ?>
PHP connection to MySQL database code
1. install the SQL Server and add MSSQL extensions for PHP
2. use the following code to connect and test
- <? Php
- $ MyServer = localhost; // host
- $ MyUser = sa; // User name
- $ MyPass = password; // password
- $ MyDB = Northwind; // MSSQL database name
- $ S = @ mssql_connect ($ myServer,
$ MyUser, $ myPass)
- Or die (Couldnt connect
SQL Server on $ myServer );
- $ D = @ mssql_select_db ($ myDB, $ s)
- Or die (Couldnt open database $ myDB );
- $ Query = SELECT TitleOfCourtesy
++ FirstName ++ LastName AS Employee;
- $ Query. = FROM Employees;
- $ Query. = WHERE Country = USA
AND Left (HomePhone, 5) = (206 );
- $ Result = mssql_query ($ query );
- $ NumRows = mssql_num_rows ($ result );
- Echo ($ NumRows = 1? : S). Returned ;
- While ($ row = mssql_fetch_array ($ result ))
- {
- Echo <li>. $ row [Employee]. </li>;
- }
- ?>
PHP connection to the Oracle database
PHP provides two sets of functions connected to Oracle: ORA _ and OCI. The ORA _ function is somewhat outdated. OCI function updates are said to be better. The syntax is almost the same. Your PHP installation options should support both.
- < ?
- if ($conn=Ora_Logon
("user@TNSNAME","password"))
- { echo "SUCCESS !
Connected to databasen";
- }else
- {echo "Failed :-(
Could not connect to databasen";}
- Ora_Logoff($conn);
- phpinfo();
- ?>
-
The above PHP connection code uses the Oracle database name, user name, and password defined in TNSNAME (specified in your tnsnames. ora file) to connect to the database. Based on successful connections, the ora_logon function returns a non-zero connection ID and stores it in the variable $ conn.
Why? Php $ mysql_server_name = 'localhost'; // change it to your mysql database server $ mysql_username = 'root'; // change it to your own my...