The example in this article describes the Zend Framework database operation method. Share it with everyone for your reference, as follows:
Example:
<?php // // SELECT * // FROM round_table // WHERE noble_title = "Sir" // ORDER BY first_name // LIMIT 10 OFFSET 20 // // 你可以使用一种重复定义的方式... $select->from('round_table', '*'); $select->where('noble_title = ?', 'Sir'); $select->order('first_name'); $select->limit(10,20); // ...或者使用一种连续定义的方式: $select->from('round_table', '*') ->where('noble_title = ?', 'Sir') ->order('first_name') ->limit(10,20); // 但是,读取数据的方法相同 $sql = $select->__toString(); $result = $db->fetchAll($sql); // 对于以上任一种方式,你都可以传送$select对象本身 // 使用Zend_Db_Select对象的 __toString()方法就可以得到查询语句 $result = $db->fetchAll($select); ?>
You can also use bound parameters in your query statement without adding quotes for the parameters yourself.
<?php // // SELECT * // FROM round_table // WHERE noble_title = "Sir" // ORDER BY first_name // LIMIT 10 OFFSET 20 // $select->from('round_table', '*') ->where('noble_title = :title') ->order('first_name') ->limit(10,20); // 读取结果使用绑定的参数 $params = array('title' => 'Sir'); $result = $db->fetchAll($select, $params); ?>
Querying multiple columns of data in the same table
When you need to query certain columns from a specified table, you can use the from() method to change the table name and column name that need to be queried. specified in this method. Both table names and column names can be replaced by aliases, and the from() method can be used as many times as needed.
<?php // 创建一个$db对象,假定adapter为Mysql $select = $db->select(); // 从some_table表中读取a,b,c三列 $select->from('some_table', 'a, b, c'); // 同样可以: $select->from('some_table', array('a', 'b', 'c'); // 从foo AS bar表中读取列bar.col $select->from('foo AS bar', 'bar.col'); // 从foo, bar两个表中读取foo.col 别名为col1,bar.col别名为col2 $select->from('foo', 'foo.col AS col1'); $select->from('bar', 'bar.col AS col2'); ?>
Multi-table joint query
When you need to perform a table joint query, you can use the join() method. First, set the table name for table union query, then the conditions for table union (ares note: this condition is for internal joins of multiple tables), and finally the column name of the query. Likewise, you can use the join() method as many times as needed.
<?php // 创建一个$db对象,假定adapter为Mysql. $select = $db->select(); // // SELECT foo.*, bar.* // FROM foo // JOIN bar ON foo.id = bar.id // $select->from('foo', '*'); $select->join('bar', 'foo.id = bar.id', '*'); ?>
WHERE condition
When you need to add a where condition, you can use the where() method. You can pass a normal query string, or you can pass a using?
<?php // 创建一个$db对象,调用SELECT方法. $select = $db->select(); // // SELECT * // FROM round_table // WHERE noble_title = "Sir" // AND favorite_color = "yellow" // $select->from('round_table', '*'); $select->where('noble_title = "Sir"'); // embedded value $select->where('favorite_color = ?', 'yellow'); // quoted value // // SELECT * // FROM foo // WHERE bar = "baz" // OR id IN("1", "2", "3") // $select->from('foo', '*'); $select->where('bar = ?', 'baz'); $select->orWhere('id IN(?)', array(1, 2, 3); ?>
GROUP BY clause
As needed, you can use the group() method multiple times to group the queried data
<?php // 创建一个$db对象,调用SELECT方法. $select = $db->select(); // // SELECT COUNT(id) // FROM foo // GROUP BY bar, baz // $select->from('foo', 'COUNT(id)'); $select->group('bar'); $select->group('baz'); // 同样可以这样调用 group() 方法: $select->group('bar, baz'); // 还可以: $select->group(array('bar', 'baz')); ?>
HAVING conditions
When you need to add having conditions to the query results, you can use the having() method. This method has the same function as the where() method.
When you call the having() method multiple times, the conditions of each having will be "merged" together for operation; if you need to implement the "or" operation, you can use the orHaving() method.
<?php // 创建一个$db对象,调用SELECT方法. $select = $db->select(); // // SELECT COUNT(id) AS count_id // FROM foo // GROUP BY bar, baz // HAVING count_id > "1" // $select->from('foo', 'COUNT(id) AS count_id'); $select->group('bar, baz'); $select->having('count_id > ?', 1); ?>
ORDER BY clause
As needed, you can use the order() method multiple times to sort the queried data
<?php // 创建一个$db对象,调用SELECT方法. $select = $db->select(); // // SELECT * FROM round_table // ORDER BY noble_title DESC, first_name ASC // $select->from('round_table', '*'); $select->order('noble_title DESC'); $select->order('first_name'); // 同样可以这样调用 order() 方法: $select->order('noble_title DESC, first_name'); // 还可以: $select->order(array('noble_title DESC', 'first_name')); ?>
Limit through the total number and offset Limitations
Zend_db_select can support limit statement restrictions at the database layer. For some databases, such as mysql and postgresql, implementing this is relatively easy because these databases natively support the "limit:count" syntax.
For some other databases, such as Microsoft's sqlserver and oracle, it is not so simple to implement the limit function, because they themselves do not support the limit statement at all. MS-SQL has a top statement to implement it, but if Oracle wants to implement the limit function, the way to write the query statement is more special. Due to the way zend_db_select works intrinsically, we can rewrite the select statement to implement the limit functionality of the above open source database system in Oracle.
To limit the returned results by setting the total number and offset of the query, you can use the limit() method, with the total value and an optional offset as parameters for calling this method.
<?php // 首先,一个简单的 "LIMIT :count" $select = $db->select(); $select->from('foo', '*'); $select->order('id'); $select->limit(10); // // 在mysql/psotgreSql/SQLite,可以得到这样的语句: // // SELECT * FROM foo // ORDER BY id ASC // LIMIT 10 // // 但是在Microsoft SQL下,可以得到这样的语句: // // SELECT TOP 10 * FROM FOO // ORDER BY id ASC // // // 现在, 是更复杂的 "LIMIT :count OFFSET :offset"方法 $select = $db->select(); $select->from('foo', '*'); $select->order('id'); $select->limit(10, 20); // // 在mysql/psotgreSql/SQLite,可以得到这样的语句: // // SELECT * FROM foo // ORDER BY id ASC // LIMIT 10 OFFSET 20 // // 但是在Microsoft SQL下,由于不支持偏移量功能,可以得到这样sql语句: // // SELECT * FROM ( // SELECT TOP 10 * FROM ( // SELECT TOP 30 * FROM foo ORDER BY id DESC // ) ORDER BY id ASC // ) // // Zend_Db_Adapter 可以自动的完成sql语句的动态创建. // ?>
LIMIT restriction through the number of pages and total number
Zend_db_select also provides a limit function for page turning. If you want to find a specific "number of pages" of results from the results, use the limitPage() method; just pass the page value you need and the number of data values displayed on each page as parameters.
<?php // 构造基础的select方法: $select = $db->select(); $select->from('foo', '*'); $select->order('id'); // ... 限制到第三页,每页包括10行数据 $select->limitPage(3, 10); // // 在MySQL/PostgreSQL/SQLite下, 可以得到: // // SELECT * FROM foo // ORDER BY id ASC // LIMIT 10 OFFSET 20 // ?>
I hope this article will be helpful to everyone’s PHP programming based on the Zend Framework framework.
For more Zend Framework database operation method examples and summary related articles, please pay attention to the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 English version
Recommended: Win version, supports code prompts!

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool
