> For the complete documentation index, see [llms.txt](https://docs.layraweb.com.tr/merhaba/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.layraweb.com.tr/merhaba/php-tarafi/konular/veritabani-islemleri/veritabani-baglantilarini-yonetme.md).

# Veritabanı Bağlantılarını Yönetme

PHP ile veritabanı işlemleri yaparken öncelikle veritabanı bağlantısının kurulması gerekmektedir. Bu işlem mysqli\_connect() veya PDO sınıfı kullanılarak gerçekleştirilebilir.

mysqli\_connect() fonksiyonu, veritabanına bağlanmak için kullanılan fonksiyondur. Aşağıdaki örnek, mysqli\_connect() fonksiyonunu kullanarak bir MySQL veritabanına bağlanmanın basit bir yolunu göstermektedir:

```php
<?php
$servername = "localhost";
$username = "kullaniciadi";
$password = "sifre";
$dbname = "veritabaniadi";

// MySQL veritabanına bağlanma
$conn = mysqli_connect($servername, $username, $password, $dbname);

// Bağlantı hatası kontrolü
if (!$conn) {
    die("Bağlantı hatası: " . mysqli_connect_error());
}
echo "Bağlantı başarılı!";
?>
```

Bu örnekte, `$servername`, `$username`, `$password` ve `$dbname` değişkenleri, bağlantı yapılacak MySQL sunucusunun adı, kullanıcı adı, şifre ve veritabanı adını tutar. mysqli\_connect() fonksiyonu, bu değişkenlerin değerlerini kullanarak veritabanına bağlanır. Bağlantı başarısız olursa, `mysqli_connect_error()` fonksiyonu hatayı döndürür ve `die()` fonksiyonu sayesinde işlem sonlandırılır.

PDO sınıfı da veritabanı bağlantılarını yönetmek için kullanılabilir. Aşağıdaki örnek, PDO sınıfını kullanarak bir MySQL veritabanına bağlanmanın basit bir yolunu göstermektedir:

```php
<?php
$servername = "localhost";
$username = "kullaniciadi";
$password = "sifre";
$dbname = "veritabaniadi";

try {
    // MySQL veritabanına bağlanma
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    // PDO hata modunu ayarlama
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    echo "Bağlantı başarılı!";
}
catch(PDOException $e) {
    echo "Bağlantı hatası: " . $e->getMessage();
}
?>
```

Bu örnekte, `$servername`, `$username`, `$password` ve `$dbname` değişkenleri, bağlantı yapılacak MySQL sunucusunun adı, kullanıcı adı, şifre ve veritabanı adını tutar. new PDO() ifadesi kullanılarak PDO sınıfı örneği oluşturulur ve `setAttribute()` fonksiyonu ile hata modu ayarlanır. Bağlantı hatası oluşursa, catch bloğu içindeki kodlar çalışır ve hata mesajı ekrana yazdırılır.

Veritabanı bağlantısı kurulduktan sonra, veritabanı işlemleri yapmak için SQL sorguları kullanılabilir. Bu sorgular


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.layraweb.com.tr/merhaba/php-tarafi/konular/veritabani-islemleri/veritabani-baglantilarini-yonetme.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
