How to connect MySQL instance with PHP _php tutorial

Source: Internet
Author: User

How to use PHP to connect to MySQL instance


A simple address book, the database name is TXL, the database has only one table called Personal_info, the table has 5 fields

Pi_idpi_namepi_telpi_qqpi_email
First we want to create a database:
Create databases TXL;
then we set up table
CREATE table ' Personal_ Info ' (
' pi_id ' bigint () not NULL auto_increment,
' pi_name ' varchar () is not null,
' pi_tel ' varchar (15) Default NULL,
' pi_qq ' varchar (all) default null,
' pi_email ' varchar () default NULL,
PRIMARY KEY (' pi_id ')
) Engine=myisam DEFAULT Charset=utf8 auto_increment=4; The SQL statement on the
is simple enough to literally guess what it means.
The following is a list of all field information connected to the database and showing the table Personal_info:
//connsql.php
!--? php
$mysql _server_name= "localhost"; Database server name
$mysql _username= "root";//Connect database user name
$mysql _password= "root";//Connect database password
$mysql _database= "LXR";// Database name

//Connect to Database
$conn =mysql_connect ($mysql _server_name, $mysql _username,
$mysql _password);

//SQL statement to extract information from table
$strsql = "SELECT * from Personal_info";
//Execute SQL query
$result =mysql_db_query ($mysql _ Database, $strsql, $conn);
//Get query Results
$row =mysql_fetch_row ($result);

Echo ';
Echo '

Show Field names
echo "\ n




















'; \ n "; for ($i =0; $i {echo ' \ n "; } echo " \ n "; Locate the first record Mysql_data_seek ($result, 0); Loop out record while ($row =mysql_fetch_row ($result)) {echo " \ n "; for ($i =0; $i {echo ' '; } echo " \ n "; } echo "
'.
Mysql_field_name ($result, $i);
echo "
';
echo "$row [$i]";
Echo '
\ n ";
echo "";
Freeing resources
Mysql_free_result ($result);
Close connection
Mysql_close ();
?>

Here is the result of the operation:
Pi_idpi_namepi_telpi_qqpi_email1zhangsan13911111111642864125zhangsan@126.com2lisi1312222222263958741lisi
@163.com3wangwu13833333333912345678wangwu@sohu.com

The so-called "original Aim", the complex operation is also based on the above, all without the above basic steps, when necessary to check the relevant manual can be resolved.

http://www.bkjia.com/PHPjc/950330.html www.bkjia.com true http://www.bkjia.com/PHPjc/950330.html techarticle How to use PHP to connect to the MySQL instance of a simple address book, the database name is TXL, the database only a table called Personal_info, the table has 5 fields pi_idpi_namepi_telpi_qqpi_e ...

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