Home  >  Article  >  Backend Development  >  I suddenly found myself a little busy this week. . Playing-PHP advanced, a little busy-php_PHP tutorial

I suddenly found myself a little busy this week. . Playing-PHP advanced, a little busy-php_PHP tutorial

WBOY
WBOYOriginal
2016-07-12 09:03:52852browse

I suddenly found myself a little busy this week. . Just playing - PHP advanced, a little busy - php

 hi

It’s only Tuesday, but I suddenly realized that this week is a bit busy, and I’m still looking forward to it – I’ll go to the city this afternoon, come back to watch a movie in the evening, have a dinner party for hot pot tomorrow night, shoot a short film in the afternoon the day after tomorrow, and maybe in the evening To play, Friday, well, it’s Friday. Although I don’t even know how to write a (bian) weekly report if this continues, it’s still a good idea to do this.

1. PHP Advanced Completion

11. Database Operation

11.1 What databases does PHP support?

PHP implements database operations by installing corresponding extensions. The design of modern applications is inseparable from the application of databases. The current mainstream databases include MsSQL, MySQL, Sybase, Db2, Oracle, PostgreSQL, Access, etc., these databases PHP Extensions can be installed to support it. Generally speaking, the LAMP architecture refers to: Linux, Apache, Mysql, and PHP. Therefore, Mysql database is widely used in PHP. We will briefly understand the operation method of Mysql in this chapter.

11.2 Database Extension

A database in PHP may have one or more extensions, including official ones and those provided by third parties. Commonly used extensions for Mysql include the native mysql library, you can also use the enhanced mysqli extension, and you can also use PDO for connection and operation.

Different extensions provide basically similar operation methods. The difference is that they may have some new features and the operation performance may be different.

Mysql extension method for database connection:

$link = mysql_connect('mysql_host', 'mysql_user', 'mysql_password');

mysqli extension:

$link = mysqli_connect('mysql_host', 'mysql_user', 'mysql_password');

PDO extension

$dsn = 'mysql:dbname=testdb;host=127.0.0.1';
$user = 'dbuser';
$password = 'dbpass';
$dbh = new PDO($dsn, $user, $password);

7ee060e1550b81159b06163c6f7f0b08';
print_r($row);
echo 'bc5574f69a0cba105bc93bd3dc13c4ec';

11.7 查询分页数据

上一节中,我们了解到通过循环可以获取一个查询的所有数据,在实际应用中,我们并不希望一次性获取数据表中的所有数据,那样性能会非常的低,因此会使用翻页功能,每页仅显示10条或者20条数据。

通过mysql的limit可以很容易的实现分页,limit m,n表示从m行后取n行数据,在PHP中我们需要构造m与n来实现获取某一页的所有数据。

假定当前页为$page,每页显示$n条数据,那么m为当前页前面所有的数据,既$m = ($page-1) * $n,在知道了翻页原理以后,那么我们很容易通过构造SQL语句在PHP中实现数据翻页。

$page = 2;
$n = 2;
$m = ($page - 1) * $n;
$sql = "select * from user limit $m, $n";
$result = mysql_query($sql);
//循环获取当前页的数据
$data = array();
while ($row = mysql_fetch_assoc($result)) {
    $data[] = $row;
}

在上面的例子中,我们使用了$m与$n变量来表示偏移量与每页数据条数,但我们推荐使用更有意义的变量名来表示,比如$pagesize, $start, $offset等,这样更容易理解,有助于团队协作开发。

a5002efef4d3ceb84fd748b6750471d1';
print_r($data);
echo 'bc5574f69a0cba105bc93bd3dc13c4ec';

11.8 更新与删除数据

数据的更新与删除相对比较简单,只需要构建好相应的sql语句,然后调用mysql_query执行就能完成相应的更新与删除操作。

$sql = "update user set name = '曹操' where id=2 limit 1";
if (mysql_query($sql)) {
    echo '更新成功';
}

同样的删除可以使用类似以下的代码:

$sql = "delete from user where id=2 limit 1";
if (mysql_query($sql)) {
    echo '删除成功';
}

对于删除与更新操作,可以通过mysql_affected_rows函数来获取更新过的数据行数,如果数据没有变化,则结果为0。

$sql = "update user set name = '曹操' where id=2 limit 1";
if (mysql_query($sql)) {
    echo mysql_affected_rows();
}

//连接数据库
mysql_connect('127.0.0.1', 'code1', '');
mysql_select_db('code1');
mysql_query("set names 'utf8'");
//预设数据以便进行更新操作
mysql_query("insert into user(name, age, class) values('王二', 19, '高三五班')");
$id = mysql_insert_id();
//在这里更新id为$id的行的名字为李白
$sql="update user set name='李白' where id=$id limit 1";
mysql_query($sql);
//输出更新数据条数
echo '数据更新行数:'.mysql_affected_rows();
mysql_query("delete from user where id='$id'");

11.9 关闭MySQL连接

当数据库操作完成以后,可以使用mysql_close关闭数据库连接,默认的,当PHP执行完毕以后,会自动的关闭数据库连接。

mysql_close();

虽然PHP会自动关闭数据库连接,一般情况下已经满足需求,但是在对性能要求比较高的情况下,可以在进行完数据库操作之后尽快关闭数据库连接,以节省资源,提高性能。

在存在多个数据库连接的情况下,可以设定连接资源参数来关闭指定的数据库连接。

$link = mysql_connect($host, $user, $pass);
mysql_close($link);

//连接数据库
$con=mysql_connect('127.0.0.1', 'code1', '');
mysql_select_db('code1');
mysql_query("set names 'utf8'");
//在这里关闭数据库连接

mysql_close($con);

 

 

 

 

 

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1077136.htmlTechArticleI suddenly found myself a little busy this week. . Playing - PHP advanced, a little busy - php hi It’s only Tuesday, but I suddenly realized that I am a little busy playing this week. I am still looking forward to going to the city this afternoon, late...
Statement:
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