PHP database call instance tutorial

Source: Internet
Author: User
In PHP development, we often need to use some database classes. Here we will briefly share the code for calling classes. For more information about php database operations, see

In PHP development, we often need to use some database classes. Here we will briefly share the code for calling classes. For more information about php database operations, see

The Code is as follows:
Require_once ("mssql. class. php ");
// 1. Create a class and connect to the database
$ Db = new mssql ("dns = aaa; uid = sa; pwd = sa; dbname = test ");
// 2. Connect to the database
$ Conn = $ db-> config ("dns = aaa; uid = sa; pwd = sa; dbname = test ");
// 3. Select a database
$ Dbname = $ db-> select_db ("test ");
// 4. Allow debugging
$ Db-> debug = true;
// 5. Execute an SQL statement that does not return results
$ Db-> execute ("insert into test01 (name) values ('This is a test! ')");
// $ Db-> exec ("");
// 6. Execute an SQL statement that returns the result
$ Rs = $ db-> query ("select * from test01 ");
// 7. Display Results in row Mode
Echo"
Display result sets in row Mode
";
While ($ r = $ db-> fetch_row ($ rs )){
Echo $ r [0]. ":". $ r [1]."
";
}

// 8. Display Results in array Mode
$ Rs2 = $ db-> query ("select * from test01 ");

Echo"
Display result sets in array Mode
";
While ($ r = $ db-> fetch_array ($ rs2 )){
Echo $ r ["id"]. ":". $ r ["name"]."
";
}

// X. Release
$ Db-> db_close ();

?>

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.