This method is supported in the MySQL 5.0+ driver. It can be used for object hydration:
<?php
$pdo_stmt = $dbh->execute('SELECT discussion.id, discussion.text, comment.id, comment.text FROM discussions LEFT JOIN comments ON comment.discussion_id = discussion.id');
foreach(range(0, $pdo_stmt->columnCount() - 1) as $column_index)
{
$meta[] = $pdo_stmt->getColumnMeta($column_index);
}
while($row = $pdo_stmt->fetch(PDO::FETCH_NUM))
{
foreach($row as $column_index => $column_value)
{
//do something with the data, using the ids to establish the discussion.has_many(comments) relationship.
}
}
?>
If you are building an ORM, this method is very useful to support more natural SQL syntax. Most ORMs require the column names to be aliases so that they can be parsed and turned into objects that properly represent has_one, has_many, many_to_many relationships.
PDOStatement->getColumnMeta
(PHP 5 >= 5.1.0, PECL pdo >= 0.2.0)
PDOStatement->getColumnMeta — Sonuç kümesindeki bir sütunla ilgili temel veriyi döndürür
Açıklama
$sütun
)Bu işlev DENEYSELDİR. Bu işlevin davranışı, ismi ve belgeleri PHP'nin sonraki sürümlerinde hiçbir duyuru yapılmaksızın değiştirilebilir. Bu riski göze alamayacaksanız bu işlevi kullanmayın.
Sütun numarası belirtilen sütunla ilgili temel verileri içeren bir ilişkisel dizi döndürür.
PDOStatement::getColumnMeta() yöntemini her veritabanı sürücüsü desteklemez.
Değiştirgeler
-
sütun -
Sütun numarası; ilk sütunun numarası 0'dır.
Dönen Değerler
Aşağıdaki alanlara sahip bir ilişkisel dizi döner:
| İsim | Değerin açıklaması |
|---|---|
| native_type | Sütun değerinin PHP veri türü. |
| driver:decl_type | Veritabanındaki sütun değerinin SQL veri türü. Eğer sonuç kümesindeki sütun bir işlevin sonucu ise bu değer PDOStatement::getColumnMeta() tarafından döndürülmez. |
| flags | Bu sütun için tanımlı seçenek kümesi. |
| isim | Sütunun ismi. |
| table | Sütunu içeren tablonun ismi. |
| len | Sütunun genişliği. Onluk gerçek sayılar dışındaki türler için normalde -1'dir. |
| precision | Sütunun sayısal hassasiyeti. Onluk gerçek sayılar dışındaki türler için normalde 0'dır. |
| pdo_type | Sütun türünü gösteren PDO::PARAM_* sabitlerinden biri. |
İstenen sütun sonuç kümesinde yoksa veya hiç sonuç kümesi yoksa FALSE
döner.
Sürüm Bilgisi
| Sürüm: | Açıklama |
|---|---|
| 5.2.3 | Dönen diziye table alanı eklendi. |
Örnekler
Örnek 1 - Sütun temel verilerinin alınması
Aşağıdaki örnekte bir PDO_SQLITE sürücüsünde bir işlev (COUNT) tarafından üretilen sonuç kümesindeki tek sütunla ilgili temel verilerin elde edilmesi gösterilmiştir.
<?php
$select = $DB->query('SELECT COUNT(*) FROM fruit');
$meta = $select->getColumnMeta(0);
var_dump($meta);
?>
Yukarıdaki örneğin çıktısı:
array(6) {
["native_type"]=>
string(7) "integer"
["flags"]=>
array(0) {
}
["name"]=>
string(8) "COUNT(*)"
["len"]=>
int(-1)
["precision"]=>
int(0)
["pdo_type"]=>
int(2)
}
Ayrıca Bakınız
- PDOStatement::columnCount() - Sonuç kümesindeki sütun sayısını döndürür
- PDOStatement::rowCount() - Son SQL deyiminden etkilenen satır sayısını döndürür
I found a short discussion related to this function at
http://www.sitepoint.com/forums/showthread.php?t=497257
I haven't tried it (yet?) but hopefully someone can find it useful.
