International PHP Conference Munich 2025

PDO_MYSQL DSN

(PECL PDO_MYSQL >= 0.1.0)

PDO_MYSQL DSNMySQL veritabanı bağlantısı

Açıklama

PDO_MYSQL Veri Kaynağı Adı (DSN) şu öğelerden oluşur:

DSN öneki

DSN öneki mysql: dizgesidir.

host

Veritabanı sunucusunu barındıran konağın ismi.

port

Veritabanı sunucusunun dinlediği portun numarası.

dbname

Veritabanının ismi.

unix_socket

MySQL Unix soketi (host veya port ile kullanılmamalıdır).

charset

Karakter kümesi. Daha ayrıntılı bilgi karakter kümesi kavramları belgelerinde bulunabilir.

Örnekler

Örnek 1 - PDO_MYSQL DSN örnekleri

Aşağıdaki örnekte MySQL veritabanlarına bağlanmak için PDO_MYSQL DSN kullanımı gösterilmiştir:

mysql:host=localhost;dbname=testdb
Daha ayrıntılı bir örnek:
mysql:host=localhost;port=3307;dbname=testdb
mysql:unix_socket=/tmp/mysql.sock;dbname=testdb

Notlar

Bilginize: Unix'e özel:

Bilgisayar adı "localhost" olarak ayarlandığında, sunucuya bağlantı Unix soketi aracılığıyla yapılır. PDO_MYSQL libmysqlclient kullanmak üzere derlenirse, soket dosyasının konumu libmysqlclient'in derlendiği konumdur. PDO_MYSQL mysqlnd kullanmak üzere derlenirse, pdo_mysql.default_socket ini yönergesi aracılığıyla öntanımlı soket ayarlanabilir.

add a note

User Contributed Notes 5 notes

up
40
codeslinger at compsalot dot com
16 years ago
I have tested this and found that the "dbname" field is optional. Which is a good thing if you must first create the db.

After creating a db be sure to exec a "use dbname;" command, or else use fully specified table references.
up
8
rhian
8 years ago
xwisdom made a mistake in his comment and got it backwards, correction below:

If you are having problems accessing a remote MYSQL database, the solution is to make sure that you add a white-space after "mysql:"

Change this...:
mysql:host=remote;

...to this:
mysql: host=remote;

See original solution here:
http://stackoverflow.com/a/25432156
up
2
divinity76 at gmail dot com
5 years ago
here is the example i prefer myself, in my opinion, this is almost always "the correct way" to do it:
<?php

$db
= new \PDO('mysql:host=localhost;dbname=testdb;charset=utf8mb4', 'username', 'password', array(
\PDO::ATTR_EMULATE_PREPARES => false,
\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION
));
up
0
fjb841 at hotmail dot com
10 days ago
This options are the best for many situations:

$options = [ PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
PDO::ATTR_CASE => PDO::CASE_LOWER];
up
0
fjb841 at hotmail dot com
10 days ago
This options are the best for many situations:

$options = [ PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
PDO::ATTR_CASE => PDO::CASE_LOWER];
To Top