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

PHP database learns how to set and get PDO properties

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

Share

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

This article focuses on "PHP database learning how to set and get PDO properties", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "PHP database learning how to set and get PDO properties" bar!

Next, let's see how to set and get the PDO property. There are many properties in the PDO object that can be used to adjust the behavior of PDO or to get the underlying driver state.

If you do not have the property options set by the last parameter in the constructor when you create the PDO object, you can set and get the values of these properties through the setAttribute () and getAttribute () methods in the PDO object after the object is created. Then let's take a look at these two methods together.

GetAttribute () method

The getAttribute () method only needs to provide a parameter to pass a specific property name that represents the parameter, and when executed successfully, it returns the value specified by the property, otherwise it returns NULL. It is easy to understand, and its syntax format is as follows:

PDO::getAttribute (int $attribute)

Among them, it should be noted that:

The parameter $attribute is one of the PDO::ATTR_* constants. There are many constants applied to database connections.

Let's take a look at the example first, and then make a summary. The example is as follows:

Output result:

From the above results, let us sum up:

PDO::ATTR_AUTOCOMMIT indicates whether PDO disables autocommit, PDO::ATTR_ERRMODE represents the current error handling mode of PDO, PDO::ATTR_CASE represents the case conversion of table field characters, and PDO::ATTR_ORACLE_NULLS indicates that the null,PDO::ATTR_PERSISTENT that converts empty strings to SQL indicates that the application acquires the data size in advance.

Not only that, let's summarize some commonly used constants used in database connections:

PDO::ATTR_CONNECTION_STATUS represents unique information related to the state of the connection.

PDO::ATTR_SERVER_INFO represents server information specific to the database.

PDO::ATTR_SERVER_VERSION represents the database server version number.

PDO::ATTR_CLIENT_VERSION represents the database client version number.

Now that you know the getAttribute () method, let's take a look at the setAttribute () method. Let's take a look at it.

SetAttribute () method

The setAttribute () method can be used to set the properties of a database handle, and setAttribute () is slightly more complex than getAttribute (). Its syntax format is as follows:

PDO::setAttribute (int $attribute, mixed $value)

One of the things we need to pay attention to is:

This method requires two parameters. The first parameter, $attribute, provides the property name specific to the PDO object, and the second parameter, $value, assigns a value to the specified property.

PDO::ATTR_CASE indicates that the column name can be forced to the specified case, where PDO::CASE_LOWER means to force the column name to be lowercase; PDO::CASE_NATURAL means to retain the column name returned by the database driver; and PDO::CASE_UPPER means to force the column name to be capitalized.

PDO::ATTR_ERRMODE is represented as an error report, where PDO::ERRMODE_SILENT indicates that only the error code is set, PDO::ERRMODE_WARNING indicates that an E_WARNING error is thrown, and PDO::ERRMODE_EXCEPTION means that an exceptions exception is thrown.

PDO::ATTR_ORACLE_NULLS is represented as available in all drivers, not limited to Oracle, converting NULL and empty strings, where PDO::NULL_NATURAL represents no conversion, and PDO::NULL_EMPTY_STRING as converting empty strings to NULL;PDO::NULL_TO_STRING and converting NULL to empty strings.

Next, let's take a look at setting the properties of the database handle using the setAttribute () method through an example, as follows:

Output result:

In the above example, you set the properties of the database handle by using the setAttribute () method. Next, let's sort out the available common property names and the values that can be used:

PDO::ATTR_STRINGIFY_FETCHES means to convert a numeric value to a string when extracting

PDO::ATTR_STATEMENT_CLASS means to set a statement class provided by a user that derives from PDOStatement. Cannot be used for persistent PDO instances.

PDO::ATTR_AUTOCOMMIT indicates whether each individual statement is automatically committed, which is available in OCI,Firebird as well as MySQL.

PDO::ATTR_DEFAULT_FETCH_MODE means to set the default extraction mode.

PDO::MYSQL_ATTR_USE_BUFFERED_QUERY means to use a buffered query, which is available in the MySQL database.

We also need to note that PDO::ATTR_TIMEOUT represents the specified number of seconds of timeout. There may be differences between different drivers, for example, when the waiting time of SQLite reaches this value, the acquisition of a writable lock will be abandoned, but other drivers may interpret this value as an interval between connection or read timeout

PDO::ATTR_EMULATE_PREPARES means to enable or disable impersonation of preprocessed statements. Some drivers do not support or support local preprocessing to a limited extent, and use this setting to force PDO to always emulate preprocessing statements, or to try to use local preprocessing statements. If the driver fails to preprocess the current query successfully, it will always return to the analog preprocessing statement.

At this point, I believe that everyone on the "PHP database learning how to set and obtain PDO properties" have a deeper understanding, might as well to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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