PHP 8.1.28 Released!

PDO::setAttribute

(PHP 5 >= 5.1.0, PHP 7, PHP 8, PECL pdo >= 0.1.0)

PDO::setAttributeBir öznitelik tanımlar

Açıklama

public PDO::setAttribute(int $öznitelik, mixed $değer): bool

Veritabanı için bir öznitelik tanımlar. Bazı temel öznitelikler aşağıda listelenmiştir. Bazı sürücülerin kendilerine özgü öznitelikleri olabilir. Sürücüye özgü özniteliklerin başka sürücülerde kullanılmamasına ise özellikle dikkat edilmelidir.

PDO::ATTR_CASE

Sütun isimlerinin harflerini belli birbüyüklüğe zorlar. Aşağıdaki değerlerden birini alabilir:

PDO::CASE_LOWER
Sütun isimlerinin harflerini küçük harfe zorlar.
PDO::CASE_NATURAL
Sütun isimlerinin harfleri veritabanı sürücüsünden döndüğü haliyle kullanılır.
PDO::CASE_UPPER
Sütun isimlerinin harflerini büyük harfe zorlar.
PDO::ATTR_ERRMODE

PDO hata raporlama kipi. Aşağıdaki değerlerden birini alabilir:

PDO::ERRMODE_SILENT
Sadece hata kodlarını atar.
PDO::ERRMODE_WARNING
Bir E_WARNING çıktılanır.
PDO::ERRMODE_EXCEPTION
PDOException istisnası yavrulanır.
PDO::ATTR_ORACLE_NULLS

Bilginize: Sadece Oracle için değil, bütün sürücüler için kullanılabilir.

null'a ve boş dizgelere dönüşümü belirler. Aşağıdaki değerlerden birini alabilir:

PDO::NULL_NATURAL
Dönüşüm yok.
PDO::NULL_EMPTY_STRING
Boş dizge null'a dönüştürülür.
PDO::NULL_TO_STRING
null boş dizgeye dönüştürülür.
PDO::ATTR_STRINGIFY_FETCHES

Döndürülürken sayısal değerler dizgeye dönüştürülür. Mantıksal bir değer gerekir: true etkinleştirir, false iptal eder.

PDO::ATTR_STATEMENT_CLASS

PDOStatement sınıfından türetilmiş kullanıcı tanımlı bir deyim sınıfı tanımlar. array(string sınıfadı, array(mixed kurucu_bağımsız değişkenler)) gerekir.

Dikkat

Kalıcı PDO bağlantılarıyla kullanılamaz.

PDO::ATTR_TIMEOUT

Zaman aşımı süresini saniye cinsinden belirtir. int türünde değer gerektirir.

Bilginize:

Tüm sürücüler bu seçeneği desteklemez ve anlamı sürücüden sürücüye farklılık gösterebilir. Örneğin, sqlite yazılabilir bir kilit almaktan vazgeçmeden önce bu zaman değerini bekler, ancak diğer sürücüler bunu bir bağlanma veya okuma zaman aşımı aralığı olarak yorumlayabilir.

PDO::ATTR_AUTOCOMMIT

Bilginize: Sadece OCI, Firebird ve MySQL'de kullanılabilir.

Her tek deyimin otomatik gönderilip gönderilmeyeceği belirtilir. Mantıksal bir değer gerekir: true etkinleştirir, false iptal eder.

PDO::ATTR_EMULATE_PREPARES

Bilginize: Sadece OCI, Firebird ve MySQL'de kullanılabilir.

Hazırlanmış deyimlerin öykünmesini etkinleştirir veya devre dışı bırakır. Bazı sürücüler yerel olarak hazırlanmış deyimleri desteklemez veya bunlar için sınırlı desteğe sahiptir. Mantıksal bir değer gerekir: PDO'yu ya her zaman hazırlanmış deyimleri öykünmeye (eğer true ve öykünmüş hazırlar sürücü tarafından destekleniyorsa) ya da yerel hazırlanmış deyimleri kullanmaya (false ise) zorlamak için bu ayar kullanılır. Sürücü mevcut sorguyu başarılı bir şekilde hazırlayamazsa, her zaman hazırlamış deyimi öykünmeye geri dönecektir.

PDO::MYSQL_ATTR_USE_BUFFERED_QUERY

Bilginize: MySQL'de kullanılır.

Tamponlu sorgu kullanımı. Mantıksal bir değer gerekir: true etkinleştirir, false iptal eder. true öntanımlıdır.

PDO::ATTR_DEFAULT_FETCH_MODE

Öntanımlı al-getir kipini ayarlar. Kipin açıklaması için PDOStatement::fetch() belgesine bakınız.

Bağımsız Değişkenler

öznitelik

Değiştirilecek öznitelik.

değer

öznitelik'e atanacak değer. Özniteliğe özgü bir tür gerekli olabilir.

Dönen Değerler

Başarı durumunda true, başarısızlık durumunda false döner.

Ayrıca Bakınız

add a note

User Contributed Notes 13 notes

up
124
colinganderson [at] gmail [dot] com
16 years ago
Because no examples are provided, and to alleviate any confusion as a result, the setAttribute() method is invoked like so:

setAttribute(ATTRIBUTE, OPTION);

So, if I wanted to ensure that the column names returned from a query were returned in the case the database driver returned them (rather than having them returned in all upper case [as is the default on some of the PDO extensions]), I would do the following:

<?php
// Create a new database connection.
$dbConnection = new PDO($dsn, $user, $pass);

// Set the case in which to return column_names.
$dbConnection->setAttribute(PDO::ATTR_CASE, PDO::CASE_NATURAL);
?>

Hope this helps some of you who learn by example (as is the case with me).

.Colin
up
30
yeboahnanaosei at gmail dot com
6 years ago
This is an update to a note I wrote earlier concerning how to set multiple attributes when you create you PDO connection string.

You can put all the attributes you want in an associative array and pass that array as the fourth parameter in your connection string. So it goes like this:
<?php
$options
= [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_CASE => PDO::CASE_NATURAL,
PDO::ATTR_ORACLE_NULLS => PDO::NULL_EMPTY_STRING
];

// Now you create your connection string
try {
// Then pass the options as the last parameter in the connection string
$connection = new PDO("mysql:host=$host; dbname=$dbname", $user, $password, $options);

// That's how you can set multiple attributes
} catch(PDOException $e) {
die(
"Database connection failed: " . $e->getMessage());
}
?>
up
19
gregory dot szorc at gmail dot com
17 years ago
It is worth noting that not all attributes may be settable via setAttribute(). For example, PDO::MYSQL_ATTR_MAX_BUFFER_SIZE is only settable in PDO::__construct(). You must pass PDO::MYSQL_ATTR_MAX_BUFFER_SIZE as part of the optional 4th parameter to the constructor. This is detailed in http://bugs.php.net/bug.php?id=38015
up
15
yeboahnanaosei at gmail dot com
6 years ago
Well, I have not seen it mentioned anywhere and thought its worth mentioning. It might help someone. If you are wondering whether you can set multiple attributes then the answer is yes.

You can do it like this:
try {
$connection = new PDO("mysql:host=$host; dbname=$dbname", $user, $password);
// You can begin setting all the attributes you want.
$connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$connection->setAttribute(PDO::ATTR_CASE, PDO::CASE_NATURAL);
$connection->setAttribute(PDO::ATTR_ORACLE_NULLS, PDO::NULL_EMPTY_STRING);

// That's how you can set multiple attributes
}
catch(PDOException $e)
{
die("Database connection failed: " . $e->getMessage());
}

I hope this helps somebody. :)
up
1
Anonymous
2 years ago
Note that contrary to most PDO methods, setAttribute does not throw a PDOException when it returns false.
up
5
steve at websmithery dot co dot uk
6 years ago
For PDO::ATTR_EMULATE_PREPARES, the manual states a boolean value is required. However, when getAttribute() is used to check this value, an integer (1 or 0) is returned rather than true or false.

This means that if you are checking a PDO object is configured as required then

<?php
// Check emulate prepares is off
if ($pdo->getAttribute(\PDO::ATTR_EMULATE_PREPARES) !== false) {
/* do something */
}
?>

will always 'do something', regardless.

Either

<?php
// Check emulate prepares is off
if ($pdo->getAttribute(\PDO::ATTR_EMULATE_PREPARES) != false) {
/* do something */
}
?>

or

<?php
// Check emulate prepares is off
if ($pdo->getAttribute(\PDO::ATTR_EMULATE_PREPARES) !== 0) {
/* do something */
}
?>

is needed instead.

Also worth noting that setAttribute() does, in fact, accept an integer value if you want to be consistent.
up
12
antoyo
13 years ago
There is also a way to specifie the default fetch mode :
<?php
$connection
= new PDO($connection_string);
$connection->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
?>
up
0
david at datava dot com
1 day ago
Note that in order for
\PDO::ATTR_TIMEOUT
to have any effect, you must set

\PDO::ATTR_ERRMODE=>\PDO::ERRMODE_EXCEPTION.

If

\PDO::ATTR_ERRMODE=>\PDO::ERRMODE_WARNING

it doesn't appear to do anything.
up
-4
rob51 at mac dot com
5 years ago
Where would I find the default values of attributes?
up
-6
guillaume at thug dot com
6 years ago
function pdo_connect(){
try {

$pdo = new PDO('mysql:host=localhost;dbname='.DB_NAME, DB_USER, DB_PASS);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->setAttribute( PDO::ATTR_EMULATE_PREPARES, false );
$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);

} catch (PDOException $e) {

die("Error!: " . $e->getMessage() . "<br/>");

}

return $pdo;
}
up
-8
justinasu at gmail dot com
8 years ago
in v5.5 PDO::MYSQL_ATTR_USE_BUFFERED_QUERY can only be set in PDO constructor, not by passing it into setAttribute.
If you set it with setAttribute it will not work. getAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY) will return 0.
up
-10
vstoykov at proab dot info
6 years ago
I am using PHP 5.6 and MySQL 5.0 on GoDaddy.
When executing a query like this:
<?php
$stmt
= $this->PDO->query("SHOW CREATE TABLE table");
?>
I get:
Uncaught exception 'PDOException' with message
'SQLSTATE[HY000]: General error: 2030 This command is not supported in the prepared statement protocol yet'
After I added:
<?php
$this
->PDO->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
?>
the query was executed successfully.
up
-15
m dot leuffen at gmx dot de
17 years ago
Hi,

if you are wondering about a size-bound (1 MB) on blob and text fields after upgrading to PHP5.1.4. You might try to increase this limit by using the setAttribute() method.

This will fail. Instead use the options array when instantiating the pdo:

$pdo = new PDO ("connection_settings", "user", "pass", array
(PDO::MYSQL_ATTR_MAX_BUFFER_SIZE=>1024*1024*50));

This should fix the problem and increase the limit to 50 MB.

Bye,
Matthias
To Top