Several common PHP database connection code examples _ PHP Tutorial

Source: Internet
Author: User
Tags php database
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

  1. <? Php
  2. $ Mysql_server_name = 'localhost ';
  3. // Change to your mysql database server
  4. $ Mysql_username = 'root ';
  5. // Change the username of your mysql database
  6. $ Mysql_password = '000000 ';
  7. // Change your mysql database password
  8. $ Mysql_database = 'mycounter ';
  9. // Change the name of your mysql database
  10. $ Conn = mysql_connect ($ mysql_server_name,
    $ Mysql_username, $ mysql_password,
    $ Mysql_database );
  11. $ SQL = 'create DATABASE mycounter
    Default character set gbk COLLATE gbk_chinese_ci;
  12. ';
  13. Mysql_query ($ SQL );
  14. $ 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 ;';
  15. Mysql_select_db ($ mysql_database, $ conn );
  16. $ Result = mysql_query ($ SQL );
  17. // Echo $ SQL;
  18. Mysql_close ($ conn );
  19. Echo "Hello! The database mycounter has been successfully created! ";
  20. ?>

PHP connection to database: PHP connection to ACCESS database code method

 
 
  1. < ?
  2. $conn = new com("ADODB.Connection");
  3. $connstr = "DRIVER={Microsoft
    Access Driver (*.mdb)};
    DBQ=". realpath("data/db.mdb");
  4. $conn->Open($connstr);
  5. $rs = new com("ADODB.RecordSet");
  6. $rs->Open("select *
    from szd_t",$conn,1,1);
  7. while(! $rs->eof) {
  8. $f = $rs->Fields(1);
  9. echo $f->value;
  10. $rs->MoveNext();
  11. }
  12. ?>

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

 
 
  1. <? Php
  2. $ MyServer = localhost; // host
  3. $ MyUser = sa; // User name
  4. $ MyPass = password; // password
  5. $ MyDB = Northwind; // MSSQL database name
  6. $ S = @ mssql_connect ($ myServer,
    $ MyUser, $ myPass)
  7. Or die (Couldnt connect
    SQL Server on $ myServer );
  8. $ D = @ mssql_select_db ($ myDB, $ s)
  9. Or die (Couldnt open database $ myDB );
  10. $ Query = SELECT TitleOfCourtesy
    ++ FirstName ++ LastName AS Employee;
  11. $ Query. = FROM Employees;
  12. $ Query. = WHERE Country = USA
    AND Left (HomePhone, 5) = (206 );
  13. $ Result = mssql_query ($ query );
  14. $ NumRows = mssql_num_rows ($ result );
  15. Echo ($ NumRows = 1? : S). Returned ;
  16. While ($ row = mssql_fetch_array ($ result ))
  17. {
  18. Echo <li>. $ row [Employee]. </li>;
  19. }
  20. ?>

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.

 
 
  1. < ?
  2. if ($conn=Ora_Logon

    ("user@TNSNAME","password"))
  3. { echo "SUCCESS !
    Connected to databasen";
  4. }else
  5. {echo "Failed :-(
    Could not connect to databasen";}
  6. Ora_Logoff($conn);
  7. phpinfo();
  8. ?>

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...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.