Home > Backend Development > PHP Tutorial > Chapter 16 PHP Operation MySQL_PHP Tutorial

Chapter 16 PHP Operation MySQL_PHP Tutorial

WBOY
Release: 2016-07-13 10:32:01
Original
844 people have browsed it

Learning points:
1. Connect PHP to MySQL
2. Add, delete, modify and query
3. Other commonly used functions

If you already have extensive experience using PHP, SQL and MySQL, you can now
combine all these technologies together. The solid integration between PHP and MySQL is just one reason why many programmers have adopted it, and another reason is that it is so simple and convenient.

1. PHP connect to MySQL

Here, we fully use UTF-8 encoding.

Set the encoding of Zend Stduio: Window -> Preferences -> Workspace

Header settings to keep Firefox and IE encoding consistent:

<?<span php
</span><span header</span>('Content-Type:text/html; charset=utf-8'<span );
</span>?>
Copy after login

Connect to MySQL

<?<span php
</span><span $conn</span> = @<span mysql_connect</span>(DB_HOST,DB_USER,<span DB_PASSWORD) or
</span><span die</span>('数据库连接失败!错误信息:'.<span mysql_error</span><span ());
</span>?>
Copy after login

Database connection parameters can be stored as constants, so they cannot be modified and are safer.

<?<span php
</span><span define</span>('DB_USER','root'<span );
</span><span define</span>('DB_PASSWORD','yangfan'<span );
</span><span define</span>('DB_HOST','localhost'<span );
</span><span define</span>('DB_NAME' ,'school'<span );
</span>?>
Copy after login

Select the database you need

<?<span php
@</span><span mysql_select_db</span>(DB_NAME) or <span die</span>('数据库找不到!错误信息:'.<span mysql_error</span><span ());
</span>?>
Copy after login

Set the character set. If it is GBK, just set SET NAMES GBK directly

<?<span php
@</span><span mysql_query</span>('SET NAMES UTF8') or <span die</span>('字符集设置错误'<span );
</span>?>
Copy after login

Get record set

<?<span php
</span><span $query</span> = "SELECT * FROM grade"<span ;
</span><span $result</span> = @<span mysql_query</span>(<span $query</span>) or <span die</span>('<span SQL 语句有误!错误信息:
</span>'.<span mysql_error</span><span ());
</span>?>
Copy after login

Output a record

<?<span php
</span><span print_r</span>(<span mysql_fetch_array</span>(<span $result</span>,<span MYSQL_ASSOC));
</span>?>
Copy after login

Release result set resources

<?<span php
</span><span mysql_free_result</span>(<span $result</span><span );
</span>?>
Copy after login

Close database

<?<span php
</span><span mysql_close</span>(<span $conn</span><span );
</span>?>
Copy after login

2. Add, delete, modify and check

New data

<?<span php
</span><span $query</span> = "<span INSERT INTO grade (name,email,point,regdate) VALUE
('李炎恢','yc60.com@gmail.com',,NOW())</span>"<span ;
@</span><span mysql_query</span>(<span $query</span>) or <span die</span>('添加数据出错:'.<span mysql_error</span><span ());
</span>?>
Copy after login
Modify data

<?<span php
</span><span $query</span> = "UPDATE grade SET name='小可爱' WHERE id=6"<span ;
@</span><span mysql_query</span>(<span $query</span>) or <span die</span>('修改出错:'.<span mysql_error</span><span ());
</span>?>
Copy after login
Delete data

<?<span php
</span><span $query</span> = "DELETE FROM grade WHERE id=6"<span ;
@</span><span mysql_query</span>(<span $query</span>) or <span die</span>('删除错误:'.<span mysql_error</span><span ());
</span>?>
Copy after login
Show data

<?<span php
</span><span $query</span> = "SELECT id,name,email,point FROM grade"<span ;
</span><span $result</span> = @<span mysql_query</span>(<span $query</span>) or <span die</span>('查询语句出错:'.<span mysql_error</span><span ());
</span><span while</span> (!!<span $row</span> = <span mysql_fetch_array</span>(<span $result</span><span )) {
    </span><span echo</span> <span $row</span>['id'].'----'.<span $row</span>['name'].'----'.<span $row</span>['email'].'----'.<span $row</span>['point'<span ];
    </span><span echo</span> '<br />'<span ;
}
</span>?>
Copy after login

3. Other commonly used functions

mysql_fetch_row(): Get a row from the result set as an enumeration array

mysql_fetch_assoc(): Get a row from the result set as an associative array
mysql_fetch_array(): Get a row from the result set as an associative array, or numeric array , or both
mysql_fetch_lengths (): Get the length of each output in the result set
mysql_field_name(): Get the field name of the specified field in the result
mysql_num_rows(): Get the number of rows in the result set
mysql_num_fields(): Get the number of fields in the result set
mysql_get_client_info(): Get the MySQL client information
mysql_get_host_info(): Get the MySQL host information
mysql_get_proto_info(): Get the MySQL protocol information
mysql_get_server_info (): Get MySQL server information

Note: The article comes from Li Yanhui’s PHP video tutorial. This article is for communication only and may not be used for commercial purposes, otherwise you will be responsible for the consequences.

http://www.bkjia.com/PHPjc/759623.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/759623.htmlTechArticleLearning points: 1. PHP connects to MySQL 2. Add, delete, modify and check 3. Other common functions if you already have them Extensive experience with PHP, SQL and MySQL, now able to combine all these technologies...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template