Reply to pdo get field names

Your name:

Reply:


Posted by John Smith on 11/17/70 11:34

I'm using PDO for the first time. I want to get the field/column names
from a SELECT query. I can get the names from the associative array
returned by fetch(). But how do I get the field names if the SELECT
query returns no rows? I could do this before with the
xxxx_fetch_field() functions.

Here's a condensed version of my code:

// query the database
$DbToQuery = new PDO("sqlite::memory:");
$DbToQuery->query("CREATE TABLE MyTable (MyField TEXT)");
$sql = "SELECT * FROM MyTable";
$result = $DbToQuery->query($sql);
// Get the number of columns
$Cols = $result->columnCount();
// Loop through the results
$countrows = 1;
while($row = $result->fetch(PDO::FETCH_ASSOC)) {
if($countrows == 1) {
// Print column names
print join(",", array_keys($row));
}
$countrows++;
// handle the row data
// ...
}

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация