Dutch PHP Conference 2025 - Call For Papers

mysql_connect

(PHP 4, PHP 5)

mysql_connectMySQL Sunucusuna bir bağlantı açar

Uyarı

Bu eklentinin kullanımı PHP 5.5.0 itibariyle önerilmemekte olup PHP 7.0.0'da kaldırılmıştır. Bu eklentinin yerine ya mysqli ya da PDO_MySQL eklentisi kullanılmalıdır. MySQL API seçerken MySQL API'ye Bakış belgesi yardımcı olabilir. Bu işlevin yerine kullanılabilecekler:

Açıklama

mysql_connect(
    string $sunucu = ini_get("mysql.default_host"),
    string $kullanıcı_adı = ini_get("mysql.default_user"),
    string $parola = ini_get("mysql.default_password"),
    bool $yeni_bağlantı = false,
    int $istemci_seçenekleri = 0
): resource|false

Bir MySQL sunucusuna bir bağlantı açar veya var olanı tekrar kullanır.

Bağımsız Değişkenler

sunucu

MySQL sunucusu. "konak_adı:port" biçiminde ayrıca bir de port içerebileceği gibi localhost için belirtildiğinde ":/bir/yol/soket" biçiminde yerel bir sokete bir yol da belirtebilir.

Eğer mysql.default_host PHP yönergesi tanımsızsa (öntanımlı), öntanımlı değer 'localhost:3306''dır. sql.safe_mode'da bu bağımsız değişken önemsenmez ve her zaman 'localhost:3306' değeri kullanılır.

kullanıcı_adı

Kullanıcı adı. Öntanımlı değer mysql.default_user ile tanımlanır. sql.safe_mode'da, bu bağımsız değişken önemsenmez ve sunucu sürecinin sahibi olan kullanıcı kullanılır.

parola

Parola. Öntanımlı değer mysql.default_password ile tanımlanır. sql.safe_mode'da, bu bağımsız değişken önemsenmez ve boş parola kullanılır.

yeni_bağlantı

Eğer mysql_connect() aynı bağımsız değişkenlerle ikinci kez çağrılırsa, yeni bir bağlantı kurulmaz, zaten açık olan bağlantının belirteci döndürülür. yeni_bağlantı bağımsız değişkeni bu davranışı değiştirir ve mysql_connect(), daha önce aynı bağımsız değişkenlerle çağrılmış olsa bile her zaman yeni bir bağlantı açar. sql.safe_mode'da bu bağımsız değişken önemsenmez.

istemci_seçenekleri

istemci_seçenekleri bağımsız değişkeni şu sabitlerin bir karışımı olabilir: 128 (LOAD DATA LOCAL tutamağı etkin), MYSQL_CLIENT_SSL, MYSQL_CLIENT_COMPRESS, MYSQL_CLIENT_IGNORE_SPACE veya MYSQL_CLIENT_INTERACTIVE. Daha fazla bilgi için MySQL istemci sabitleri ile ilgili bölümü okuyun. sql.safe_mode'da bu bağımsız değişken önemsenmez.

Dönen Değerler

Başarı durumunda bir MySQL bağlantı belirteci döndürür, hata durumunda false döndürür.

Örnekler

Örnek 1 - mysql_connect() örneği

<?php
$link
= mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!
$link) {
die(
'Bağlanamadı: ' . mysql_error());
}
echo
'Başarıyla bağlandı';
mysql_close($link);
?>

Örnek 2 - konak_adı:port sözdizimini kullanan mysql_connect() örneği

<?php
// 3307 portundan example.com'a bağlanıyoruz
$link = mysql_connect('example.com:3307', 'mysql_user', 'mysql_password');
if (!
$link) {
die(
'Bağlanamadı: ' . mysql_error());
}
echo
'Başarıyla bağlandı';
mysql_close($link);

// 3307 portundan localhost'a bağlanıyoruz
$link = mysql_connect('127.0.0.1:3307', 'mysql_user', 'mysql_password');
if (!
$link) {
die(
'Bağlanamadı: ' . mysql_error());
}
echo
'Başarıyla bağlandı';
mysql_close($link);
?>

Örnek 3 - ":/bir/yol/soket" sözdizimini kullanan mysql_connect() örneği

<?php
// localhost ve sokete, örneğin /tmp/mysql.sock, bağlanıyoruz

// 1. yol: localhost'u ihmal ederek
$link = mysql_connect(':/tmp/mysql', 'mysql_user', 'mysql_password');
if (!
$link) {
die(
'Bağlanamadı: ' . mysql_error());
}
echo
'Başarıyla bağlandı';
mysql_close($link);


// 2. yol: localhost ile
$link = mysql_connect('localhost:/tmp/mysql.sock', 'mysql_user', 'mysql_password');
if (!
$link) {
die(
'Bağlanamadı: ' . mysql_error());
}
echo
'Başarıyla bağlandı';
mysql_close($link);
?>

Notlar

Bilginize:

Sunucu olarak "localhost" veya "localhost:port" belirttiğinizde, MySQL istemci kütüphanesi bunu geçersiz kılar ve yerel bir sokete (Windows'ta boru hattı (pipe) denir) bağlanmaya çalışır. TCP/IP kullanılmak istenirse, "localhost" yerine "127.0.0.1" kullanılır. Eğer MySQL istemci kütüphanesi yanlış bir yerel sokete bağlanmaya çalışırsa, doğru yolun php.ini dosyasında mysql.default_host yönergesine atanması ve sunucu alanının boş bırakılması gerekir.

Bilginize:

Sunucuya bağlantı, eğer daha önceden mysql_close() kullanılarak kapatılmadıysa, betiğin çalışması biter bitmez kapatılır.

Bilginize:

"Can't create TCP/IP socket (10106)" hatası genellikle variables_order yapılandırma yönergesinin E harfini içermediği anlamına gelir. Windows'ta, ortam kopyalanmadıysa SYSTEMROOT ortam değişkeni kullanılamaz ve PHP Winsock'u yüklerken sorun yaşar.

Ayrıca Bakınız

add a note

User Contributed Notes 3 notes

up
1
nicodenboer at yahoo dot com
12 years ago
Be carefull here if you use utf8.

The file db.opt of your database should contain the following lines:
default-character-set=utf8
default-collation=utf8_general_ci

It means that your database is created to use the utf8 characterset.
One way to accomplish this is:
CREATE DATABASE my_database DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;

Then, after connecting to it from PHP you should use:
mysql_set_charset("UTF8", $connection);

If you don't do this, you will get ugly problems in case other software is reading and writing to the same database!!!!!!
up
0
VTool
7 years ago
fcgid_module modules/mod_fcgid.so
FcgidMaxRequestLen 209715200
FcgidConnectTimeout 240
FcgidIOTimeout 240
FcgidBusyScanInterval 240
FcgidBusyTimeout 240
# Esta línea instruye al servidor web para que reconozca un tipo nuevo (php)
AddHandler fcgid-script .php
# Esta línea indica al servidor web donde está instalado PHP.
FcgidInitialEnv PHPRC "c:/php"
# Esta línea indica al servidor web que debe ejecutar la aplicación
# php-cgi.exe cuando un cliente (navegador) solicite una página con
# extensión .php
FcgidWrapper "c:/php/php-cgi.exe" .php
# Con esta línea damos los permisos necesarios para que los clientes puedan
# acceder/ejecutar a los archivos .php
<Directory "c:/Apache/htdocs">
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride None
Allow from all
</Directory>
up
-5
cory dot mawhorter gmail.com
15 years ago
Hopefully this saves someone some grief.

My dev computer is windows and runs wampserver. I have frequent problems with PHP being unable to connect to MySQL after periods of extreme DB activity.

Long story short, it was because I was not running mysql via named-pipes and Windows was running out of available ports to serve PHP. Apparently, on windows, you have 5000 ports to work with and once they are opened, they remain so for 120 seconds before being released. This causes problems with mysql/networking because a new port is requested for each connection.

You can read more about the problem at:
(Link too long and had to be broken up)
http://dev.mysql.com/doc/refman/5.0/en
/can-not-connect-to-server.html#can-not-connect-to-server-on-windows
?>

Since mysql is on localhost, I can just enable named-pipes (which is how you should have mysql setup if you don't need networking) to get around the problem instead of the workaround listed on that page.

For details, see:
http://dev.mysql.com/tech-resources
/articles/securing_mysql_windows.html
To Top