PHP to get started PHP operation MySQL DB instance code _php tutorial

Source: Internet
Author: User
This is a simple PHP and MySQL data query data Simple introductory article, we talked about the most important functions of the detailed introduction, the need for students to see.
The code is as follows Copy Code

Link Database

$conn = @mysql_connect ("localhost", "root", "88888888") or Die ("link error");

/*
The mysql_connect () function opens a non-persistent MySQL connection.

Grammar
mysql_connect (server,user,pwd,newlink,clientflag) parameter description
Server is optional. Specifies the server to connect to.

You can include a port number, such as "Hostname:port", or a path to a local socket, such as ":/path/to/socket" for localhost.

If PHP directive mysql.default_host is undefined (by default), the default value is ' localhost:3306 '.

User is optional. User name. The default value is the user name of the server process owner.
PWD is optional. Password. The default value is a blank password.

*/

The code is as follows Copy Code

Solve Chinese garbled characters

mysql_query ("Set names ' GBK '");

Open Database
mysql_select_db ("WordPress", $conn) or Die ("Open failed");
$sql = "SELECT * from Wp_posts";

The mysql_select_db () function sets the active MySQL database.

If successful, the function returns True. If it fails, it returns false.

Grammar
mysql_select_db (database,connection) parameter description
Database Required. Specifies the database to select.
Connection is optional. Specify MySQL connection. If not specified, the previous connection is used.

The code is as follows Copy Code

Execute SQL statement
$result = mysql_query ($sql, $conn);

Required parameters for loop printing
while ($row = Mysql_fetch_array ($result))
{
Echo $row [4]. "
";//test multiple times, $row [4] can only be indexed, cannot be a column name, depressed in ing
}

The Mysql_fetch_array () function takes a row from the result set as an associative array, or as a numeric array, or both

Returns an array based on the rows obtained from the result set, or False if there are no more rows.

Grammar
Mysql_fetch_array (data,array_type) parameter description
Data is optional. Specifies the data pointers to be used. The data pointer is the result of the mysql_query () function.
Array_type is optional. Specifies what kind of results to return. Possible values:

MYSQL_ASSOC-Associative arrays
Mysql_num-numeric array
Mysql_both-Default. Simultaneous generation of associative and numeric arrays

http://www.bkjia.com/PHPjc/629152.html www.bkjia.com true http://www.bkjia.com/PHPjc/629152.html techarticle This is a simple PHP and MySQL data query data Simple introductory article, we talked about the most important functions of the detailed introduction, the need for students to see. The code is as follows ...

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