phpstudy2--php script Access MySQL database

Source: Internet
Author: User
Tags php script web hosting

Objective:

In front of the introduction of PHP script to obtain and print output HTML submitted data, this article will introduce the PHP script access query MySQL database.

Example code:

1<style>2td{width:100px;}/*I exist only to prove that the style style can be defined outside the PHP script in the php file*/3Th{background:#22AAFF; Text-align:left;color: #fff;;}4</style>5<?PHP6     Header("Content-type:text/html;charset=utf8"); 7     8     $link=mysql_connect("localhost", "root", "root");9     mysql_select_db("Test",$link);//Select DatabaseTen      One     $q= "SELECT * from User_info";//SQL query Statements A     mysql_query("SET NAMES UTF8");//solve Chinese garbled problem -     $rs=mysql_query($q);//Get Data Set -     if(!$rs){ die("Valid result!");} the     Echo"<table>"; -     Echo"<tr><th> number </th><th> name </th><th> mobile number </th></tr>"; -      while($row=Mysql_fetch_array($rs))Echo"<tr><td>$row[0]</td><td>$row[1]</td><td>$row[2]</td></tr> ";//Show Data -     Echo"</table>"; +     Mysql_free_result($rs);//To close a data set -?>

Example parsing:

1. The header () function sends the original HTTP header to the client.

1 // define the HTML page and set the encoding 2 Header ("Content-type:text/html;charset=utf8"34// export Excel table when setting document type and export file name extension  5header ("Content-type:application/vnd.ms-excel" ); 6 Header ("Content-disposition:filename=csat.xls");

2. Connect to the database:

$link=mysql_connect("localhost", "root", "root"); mysql_select_db $link // Select Database
Example parsing:
Connection database mysql_connect (hostname, user name, password);

Select Database mysql_select_db (database name, database connection);

3. Query the data table to get the data set:

1 $sql // SQL query Statements 2     mysql_query("SET NAMES UTF8");   solve Chinese garbled problem 3     $resultmysql_query($sql//  Get Data set

4. Output table

1     Echo "<table>"; 2     Echo "<tr><th> number </th><th> name </th><th> mobile number </th></tr>"; 3     Echo "<tr><td>1</td><td> Zhang San </td><td>13888888888</td></tr>"; 4     Echo "<tr><td>1</td><td> Zhang San </td><td>13888888888</td></tr>"; 5     Echo "</table>";

5. Cyclic output data

1  while ($rowmysql_fetch_array($rsecho "<tr><td>$row[0]</td ><td>$row[1]</td><td>$row// display Data

Example parsing:

$row =mysql_fetch_array ($RS); Reading a dataset and returning an array
while ($row =mysql_fetch_array ($rs)) {//loops through the data in the array
......
}
Note: Iterate through the contents of the dataset and be sure to use the while loop.

Precautions:

php5.5 has abolished the MySQL extension and needs to be changed to mysqli or PDO, the sample code is as follows:

1 //pdo Way2     $DBH=NewPDO (' Mysql:host=localhost;dbname=test ',$user,$pass); 3 //mysqli Way4     $link=Mysqli_connect( 5' localhost ',/*The host to connect to connects to the MySQL address*/  6' Root ',/*The user to connect as connects MySQL username*/  7' Root ',/*The password to use connection mysql password*/  8' Test ');/*The default database to query connects to the DB name*/  9       if(!$link)Echo"No connection succeeded!";

Of course, the front of the usage is still available, most web hosts are supported by default, if the web hosting default supported PHP version 5.5 above, then you can set a lower version of PHP, as below, using Jspstudy to set different PHP version:

phpstudy2--php script Access MySQL database

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.