A detailed explanation of MySQL operation in PHP

Source: Internet
Author: User

Next to bring you a few days to learn PHP and mysql some knowledge, very practical and very specific, covering all common PHP in the built-in method of MySQL.

Database connection:

mysql_connect (' localhost ', ' root ', ' root ');// three parameters are database address, database user name and password, respectively

Set the MySQL link identifier $con =mysql_connect (' localhost ', ' root ', ' root ')

Close the database by $con :mysql_close ($con);

Open a database:mysql_select_db ("database")// parameter is the name of the database to select

Executes an SQL statement:mysql_query (' insert into test (name) VALUES ("abc") ');

by printing mysql_error (); easy to see where an error occurred

mysql_query (' Set names UTF8 '); tell the database that the operations are UTF8 encoded format input and output

Mysql_fetch_row () fetching data as an indexed array

$query = mysql_query (' SELECT * from test ');// successful return of resource identifier (the address where the data returned by the statement is executed)

Mysql_fetch_row ($query);// Returns an array that displays information about the first data queried

If you want to output all the data that you have queried,

while ($row = Mysql_fetch_row ($query)) {Print_r ($row);} loop Print all queries to the information (an array of one array to print)

Thatmysql_fetch_row ()each time it executes, it takes one piece of data from the result set at a time. The index value of each array is related to the order of the fields. For example, a data hasname,Sex,Passwordfields, thearray[0]=>Shownamevalues, and so on. Therefore, the above statement can be passed directly through the$row [0]Getnameand so on.

Mysql_fetch_array () getting data in a mixed array form

$arr = Mysql_fetch_array ($query); After this is used, $attr [' name '] you can take it directly . name the value of the field

$arr = Mysql_fetch_array ($query, MYSQL_ASSOC); This will eliminate those arrays of indexed values, leaving only the part of the corresponding field name.

MYSQL_ASSOC: Returns an associative array (returns only an array of associated fields)

Mysql_num: Returns an array of numbers ( as mysql_fetch_row returns)

Mysql_both: Returning a mixed array

MYSQL_FETCH_ASSOC () An associative array form to get an array

Mysql_fetch_object () fetching and displaying data as an object

How to take a value:

$arr = Mysql_fetch_object ($query);

$arr, name; can be taken to name the value

Mysql_num_rows (): Gets the number of rows in the result set, returning a number. Can be used for data output before the judgment (with the if statement, no data when the output, save resources)

Mysql_result (); returns the value of a field in the result set

Mysql_result ($query, 0,1);// The second parameter specifies the number of rows to be taken, and the third Uncle specifies the field (offset) of the first. This way, the value of selecting a field is implemented.

Mysql_affected_rows: Gets the number of rows of the record affected by the previous operation (that is, several lines have been modified)


A detailed explanation of MySQL operation in PHP

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.