> 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/veritabanina-baglanma.md).

# Veritabanına Bağlanma

PHP'de veritabanı işlemleri yapabilmek için öncelikle veritabanına bağlanmamız gerekiyor. Bunun için `mysqli_connect()` veya `PDO` gibi fonksiyonlar kullanabiliriz.

`mysqli_connect()` fonksiyonu ile MySQL veritabanına bağlanmak için aşağıdaki gibi bir örnek kullanılabilir:

```php
$servername = "localhost";
$username = "kullanici_adi";
$password = "parola";
$dbname = "veritabani_adi";

// Bağlantı oluşturma
$conn = mysqli_connect($servername, $username, $password, $dbname);

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

Bu örnekte `$servername` değişkeni veritabanının bulunduğu sunucu adresini, `$username` ve `$password` değişkenleri veritabanı kullanıcısının adını ve şifresini, `$dbname` değişkeni de veritabanının adını içermektedir. Bağlantı oluşturulduktan sonra `mysqli_connect_error()` fonksiyonu ile bir hata olup olmadığı kontrol edilir ve bağlantı başarılı mesajı ekrana yazdırılır.

PDO ile MySQL veritabanına bağlanmak için ise aşağıdaki gibi bir örnek kullanılabilir:

```php
$servername = "localhost";
$username = "kullanici_adi";
$password = "parola";
$dbname = "veritabani_adi";

try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    // Bağlantıyı ayarla
    $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 de `$servername`, `$username`, `$password` ve `$dbname` değişkenleri yine kullanılmaktadır. Bağlantı oluşturulurken `new PDO()` ifadesi kullanılır ve `$servername` ve `$dbname` değişkenleri `mysql:host=` ve `dbname=` ile birleştirilerek verilir. Bağlantı oluşturulduktan sonra hata kontrolü yapılarak bağlantı başarılı mesajı veya hata mesajı ekrana yazdırılır.

Veritabanına bağlanırken kullanıcı adı ve şifre gibi bilgilerin açık olarak kod içinde yer almaması için bu bilgiler genellikle bir konfigürasyon dosyasında saklanır ve kod içinde bu dosyadan okunarak kullanılır.


---

# 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/veritabanina-baglanma.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.
