Home >Backend Development >PHP Tutorial >In-depth analysis of PDO::setAttribute in PHP

In-depth analysis of PDO::setAttribute in PHP

autoload
autoloadOriginal
2021-04-23 11:36:541998browse

In-depth analysis of PDO::setAttribute in PHP

##PHP Using PDO to connect to the database is a basic operation. After using PDO to connect to the database, what you get is# For ##Statement type objects, we can use the setAttribute() method to set the attributes of the database handle. This article will take you to take a look.

1. First, let’s take a look at the syntax of setAttribute():

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

    $attribute: Provide the specific attribute name of the PDO object
  • $value: Assign a value to the specified attribute
  • The return value is a bool value

2. About the attributes of $attribute

    PDO::ATTR_CASE: Force column names to the specified case
  • PDO::ATTR_ERRMODE: Error reporting.
  • ##PDO::ATTR_ORACLE_NULLS (available in all drivers, not limited to Oracle): Convert NULL and empty strings.
  • PDO::ATTR_STRINGIFY_FETCHES: Convert the value to a string when extracting
  • PDO::ATTR_STATEMENT_CLASS: Set the user-provided values ​​derived from PDOStatement statement class. Requires array
  • PDO::ATTR_TIMEOUT: Specifies the number of seconds for timeout.
  • PDO::ATTR_AUTOCOMMIT Whether to automatically submit each individual statement.
  • PDO::ATTR_EMULATE_PREPARES Enables or disables simulation of prepared statements.
  • PDO::MYSQL_ATTR_USE_BUFFERED_QUERY (available in MySQL): Use buffered queries.
  • PDO::ATTR_DEFAULT_FETCH_MODE: Set the default extraction mode.
3. Take PDO::ATTR_CASE as an example:

<?php
$servername = "localhost";
$username = "root";
$password = "root123456";
$dbname   = "my_database";
$pdo = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
echo "连接成功"."<br>"; 

$pdo->setAttribute(PDO::ATTR_CASE, PDO::CASE_UPPER);
echo $pdo -> getAttribute(PDO::ATTR_CASE);
$pdo->setAttribute(PDO::ATTR_CASE, PDO::CASE_LOWER);
echo $pdo -> getAttribute(PDO::ATTR_CASE);
$pdo->setAttribute(PDO::ATTR_CASE, PDO::CASE_NATURAL);
echo $pdo -> getAttribute(PDO::ATTR_CASE);
?>
输出:  1
        2
        0
Recommended:

2021 PHP interview questions summary (collection)》《php video tutorial

The above is the detailed content of In-depth analysis of PDO::setAttribute in PHP. For more information, please follow other related articles on the PHP Chinese website!

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