PHP access MySQL database beginner's _php tutorial

Source: Internet
Author: User
In the background of the site, often to deal with the database. This article describes how to use XAMPP to manage MySQL databases and how to access MySQL databases with PHP.

A Using XAMPP to manage MySQL databases

First use XAMPP to open the admin page for MySQL. The steps are as follows: After starting XAMPP click Admin to enter XAMPP for Windows Main page, click phpMyAdmin in the Main page.




After entering the phpMyAdmin page, create a new database test and create a t_student table in this database, table total three fields, number ID, name name, age.



You can then start using PHP to access the MySQL database. Since PHP has been well encapsulated in accessing MySQL databases, accessing MySQL with PHP is a very easy task.

Two PHP Access MySQL Database

The following PHP program accesses the T_student table in the test database, reads the data, and outputs the data in tabular form. All program code is as follows:

by Morewindows (http://blog.csdn.net/MoreWindows)
Defining constants
Define (db_host, ' localhost ');
Define (db_user, ' root ');
Define (Db_pass, ' 111111 ');
Define (db_databasename, ' test ');
Define (db_tablename, ' t_student ');
column names for database tables
$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);

Reads the number of records in a 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");
}
echo "table with $count[0] Records
";


$sql = sprintf ("Select%s from%s", Implode (",", $dbcolarray), db_tablename);
$result = mysql_query ($sql, $conn);
Form
Echo '















';//table Header $thstr = " ""; echo $thstr;//table contents while ($row =mysql_fetch_array ($result, MYSQL_ASSOC))//with $ROW=MYSQL_FETCH_ASSOC ($result) equivalent {echo ' "; $tdstr = ""; foreach ($dbcolarray as $TD) $tdstr. = " "; Echo $tdstr; echo " ";} echo "
" . Implode (" ", $dbcolarray).
$row [$TD]
";
Mysql_free_result ($result);
Mysql_close ($conn);
The results of the operation are as follows:


This HTML element is completely output by PHP in the original way, the next article will be used Smarty to complete the task of reading the database and add the "jquery table add delete and modify and set the odd and even row color" in the Set table parity Line Color function added.

Excerpted from Morewindows


http://www.bkjia.com/PHPjc/478477.html www.bkjia.com true http://www.bkjia.com/PHPjc/478477.html techarticle in the background of the site, often to deal with the database. This article describes how to use XAMPP to manage MySQL databases and how to access MySQL databases with PHP. A Use XAMPP to manage MySQL data ...

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