PHP accessing MySql database

Source: Internet
Author: User
In the website background, you often have to deal with databases. This article describes how to use XAMPP to manage MySql Databases and How to Use PHP to access MySql databases. 1. Use XAMPP to manage MySql databases first use XAMPP to open the MySql Management page. Follow these steps: Start XAMPP and click Admin to go to The XAMPPforWindows homepage.

In the website background, you often have to deal with databases. This article describes how to use XAMPP to manage MySql Databases and How to Use PHP to access MySql databases. 1. Use XAMPP to manage MySql databases first use XAMPP to open the MySql Management page. Follow these steps: Start XAMPP and click Admin to go to the home page of XAMPP for Windows.

In the website background, you often have to deal with databases. This article describes how to use XAMPP to manage MySql Databases and How to Use PHP to access MySql databases.

1. Use XAMPP to manage MySql Databases

Use XAMPP to open the MySql Management page. Follow these steps: Start XAMPP and click Admin to go to the home page of XAMPP for Windows. Click phpMyAdmin on the home page.

On the phpMyAdmin page, create a database test and create a t_student table in the database. The table contains three fields: id, name, and age.

Then you can start to use PHP to access the MySql database. Since PHP has encapsulated the MySql database, it is very easy to access MySql with PHP.

Ii. PHP accessing MySql database

The following PHP program accesses the t_student table in the test database, reads data, and outputs data in the form of tables. The Code is as follows:

// By MoreWindows (http://blog.csdn.net/MoreWindows) // defines the constant define (DB_HOST, 'localhost'); define (DB_USER, 'root'); define (DB_PASS, '123 '); define (DB_DATABASENAME, 'test'); define (DB_TABLENAME, 't_ student '); // name of the database table column $ dbcolarray = array ('id', 'name ', 'age'); // mysql_connect $ conn = mysql_connect (DB_HOST, DB_USER, DB_PASS) or die ("connect failed ". mysql_error (); mysql_select_db (DB_DATABASENAME, $ conn); // read the number of records in the table $ SQL = sprintf ("select count (*) from % s", DB_TABLENAME ); $ result = mysql_query ($ SQL, $ conn); if ($ result) {$ count = mysql_fetch_row ($ result);} else {die ("query failed ");} the echo table contains $ count [0] records.
"; $ SQL = sprintf (" select % s from % s ", implode (", ", $ dbcolarray), DB_TABLENAME); $ result = mysql_query ($ SQL, $ conn); // table echo' '; // Header $ thstr =" "; Echo $ thstr; // while ($ row = mysql_fetch_array ($ result, MYSQL_ASSOC) in the table content // equivalent to $ row = mysql_fetch_assoc ($ result) {echo" "; $ Tdstr =" "; foreach ($ dbcolarray as $ td) $ tdstr. =" "; Echo $ tdstr; echo" ";} Echo"
". Implode (" ", $ Dbcolarray )."
$ Row [$ td]
"; Mysql_free_result ($ result); mysql_close ($ conn );

The running result is as follows:

This HTML element is completely output by PHP. This section describes how to use smarty to read the database and 《Add, delete, modify, and set the color of the odd and even rows in the jquery table.To set the color of the table's parity rows.

Http://blog.csdn.net/morewindows/article/details/7086524

Related Article

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.