Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use PDO to get query results in PHP Database

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "how to use PDO to obtain query results in PHP database". In daily operation, I believe that many people have doubts about how to use PDO to obtain query results in PHP database. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubt of "how to use PDO to obtain query results in PHP database". Next, please follow the editor to study!

As long as the SELECT query is executed successfully, the result set object will be generated. Whether using the qurey () method in the PDO object or the preprocessing statement combining prepare () and execute (), executing the SELECT query will result in the result set object PDOStatement.

You can get the query results of the SELECT statement through the methods in the PDOStatement class, so let's take a look at several common methods to get result set data in the PDOStatement class.

Fetch () method

The fetch () method can get the contents of the current row from the result set of a PDOStatement object, move the result set pointer to the next row, and return FALSE when the end of the result set is reached. The syntax format of this method is as follows:

PDOStatement::fetch ([int $fetch_style [, int $cursor_orientation = PDO::FETCH_ORI_NEXT [, int $cursor_offset = 0])

Among them, it should be noted that:

$fetch_style represents an optional parameter that controls how the next line is returned to the caller. Where the value of this parameter must be one of the PDO::FETCH_* series constants, as follows:

PDO::FETCH_ASSOC indicates that an associative array is returned

PDO::FETCH_BOTH (default) returns an array of indexed and associative arrays

PDO::FETCH_BOUND means to return TRUE and assign the value in the result set to the PHP variable bound by the PDOStatement::bindColumn () method

PDO::FETCH_OBJ represents an anonymous object that returns a property name corresponding to the result set column name.

PDO::FETCH_CLASS: returns a new instance of the request class, mapping the column names in the result set to the corresponding attribute names in the class.

If fetch_style contains PDO::FETCH_CLASSTYPE such as: PDO::FETCH_CLASS | PDO::FETCH_CLASSTYPE, the class name is determined by the value of the first column

Cursor orientation represents an optional parameter to determine which row should be fetched when the object is a scrollable cursor. This value must be one of the PDO::FETCH_ORI_* series constants and defaults to PDO::FETCH_ORI_NEXT.

$offset represents the optional parameter, which specifies the absolute row number of the desired row in the result set when the parameter $cursor_orientation is set to PDO::FETCH_ORI_ABS, and the position of the desired row relative to the cursor before calling PDOStatement::fetch () when the parameter $cursor_orientation is set to PDO::FETCH_ORI_REL.

Next, let's take a look at using the fetch () method to get the query results of the SELECT statement. Examples are as follows:

Output result:

So we get the contents of the current row from the result set of a PDOStatement object through the fetch () method. $offset represents the optional parameter, which specifies the absolute row number of the desired row in the result set when the parameter $cursor_orientation is set to PDO::FETCH_ORI_ABS, and the position of the desired row relative to the cursor before calling PDOStatement::fetch () when the parameter $cursor_orientation is set to PDO::FETCH_ORI_REL.

Next let's take a look at the application of the fetchAll () method.

FetchAll () method

The fetchAll () method is similar to the fetch () method described above, but it only needs to be called once to get all the rows in the result set and assign it to the returned array. The syntax format of this method is as follows:

PDOStatement::fetchAll ([int $fetch_style [, mixed $fetch_argument [, array $ctor_args = array ()])

Among them, it should be noted that:

Fetch_style represents an optional parameter that controls the contents of the returned array. The default value is PDO::FETCH_BOTH. The value of this parameter is the same as the fetch () method

$fetch_argument according to the value of the $fetch_style parameter, this parameter has a different meaning:

PDO::FETCH_COLUMN: returns the column that specifies the index starting with 0

PDO::FETCH_CLASS: returns an instance of the specified class, mapping the columns of each row to the corresponding attribute names in the class

PDO::FETCH_FUNC: passes the column of each row as an argument to the specified function and returns the result after the function is called.

$ctor_args represents the parameter of the constructor of the custom class when the $fetch_style argument is PDO::FETCH_CLASS.

Next, let's take a look at the practical application of the fetchAll () method through an example, as follows:

Output result:

So we get the query result of the SELECT statement by using the fetchAll () method. $ctor_args represents the parameter of the constructor of the custom class when the $fetch_style argument is PDO::FETCH_CLASS.

Next let's take a look at the use of the fetchColumn () method.

FetchColumn () method

The fetchColumn () method gets the value of the specified field in the current row in the result set, and its syntax format is as follows:

PDOStatement::fetchColumn ([int $column_number = 0])

Among them, it should be noted that:

The parameter $column_number represents the index number of the column you want to retrieve from the row.

If the parameter has no value, that is, if no value is provided, it will be obtained from the first column.

Next, let's take a look at the use of the fetchColumn () method through an example, as follows:

Output result:

So we get the value of the specified field by using the fetchColumn () method.

At this point, the study on "how to use PDO to obtain query results in PHP database" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report