Getting started with php to operate mysql database instance code

Source: Internet
Author: User
Tags getting started with php socket mysql database
The code is as follows: Copy code

// Link to the database

$ Conn = @ mysql_connect ("localhost", "root", "88888888") or die ("link error ");

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

Syntax
Mysql_connect (server, user, pwd, newlink, clientflag) parameter description
Optional. Specifies the server to be connected.

It can include the port number, for example, "hostname: port", or the path to the local socket, for example, ":/path/to/socket" for localhost ".

If the PHP command mysql. default_host is not defined (the default value is true), the default value is 'localhost: 3306 '.
 
Optional. User name. The default value is the user name of the server process owner.
Optional. Password. The default value is null.

*/

The code is as follows: Copy code

// Solve Chinese garbled characters

Mysql_query ("set names 'gbk '");

// Open the database
Mysql_select_db ("wordpress", $ conn) or die ("Opening failed ");
$ SQL = "select * from wp_posts ";

The mysql_select_db () function sets the active MySQL database.

If yes, the function returns true. If it fails, false is returned.

Syntax
Mysql_select_db (database, connection) parameter description
Database is required. Specifies the database to be selected.
Connection is optional. MySQL connection is required. If not specified, the previous connection is used.

 

The code is as follows: Copy code

// Execute the SQL statement
$ Result = mysql_query ($ SQL, $ conn );

// Parameters required for loop printing
While ($ row = mysql_fetch_array ($ result ))
{
Echo $ row [4]. "<br>"; // multiple quizzes. $ row [4] can only be an index but cannot be a column name.
}

The mysql_fetch_array () function retrieves a row from the result set as an associated array, or an array of numbers, or both.

Returns the array generated based on the rows obtained from the result set. If no more rows exist, false is returned.

Syntax
Mysql_fetch_array (data, array_type) parameter description
Data is optional. Specifies the data pointer to be used. This data pointer is produced by the mysql_query () function.
Array_type is optional. Specifies the result to be returned. Possible values:

MYSQL_ASSOC-associated array
MYSQL_NUM-number array
MYSQL_BOTH-default. Generate join and numeric arrays at the same time

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.