473,385 Members | 2,210 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

pdo get field names

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
// ...
}
Dec 13 '05 #1
0 2663

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Michael Flanagan | last post by:
(Bottom line: I think what I'm looking for is an easy way of changing the case of key values in an array.) I've got code that I'm trying to make agnostic about the underlying database system I'm...
1
by: Richard | last post by:
I need to dynamically add (but not remove) vectors of doubles based on keys/names. Please see my code below. Am I reinventing the wheel or doing anything inefficiently? I get the feeling that I am,...
1
by: Earl Anderson | last post by:
I have imported an Excel worksheet into A97/WinXPH which had the new employees names in one field ( in a Last Name,First Name configuration). I wanted to split that one field ( ) into two...
1
by: Don Leverton | last post by:
Hi Folks, I have been given a CD with approx 130 .xls files (bean-counters!) that I would like to import and merge to ONE table (tblTradeshow). The XL files are *similarly*, but not...
6
by: HD | last post by:
Hello. For the following, I would appreciate if anyone could tell me: if it can be done, how it might done, and/or what search terms I could use to find the solution myself. I would like to...
2
by: Ralph | last post by:
I'm trying to import a range of cells from an Excel spreadsheet into a table in access. The first row contains column labels, but I cannot use those as my field names, both because of their format...
7
by: Tizzah | last post by:
What is wrong with that? regex = /^(http|https):\/\/+({1}+)*\.{2,5}(({1,5})?\/.*)?$/ if(field.hpage.value != regex.test(field.hpage.value)){ alert("Bad Homepage") field.hpage.focus()...
4
by: | last post by:
Given an XML file (dataset.writexml), here is my output (simplified for this posting): <?xml version="1.0" standalone="yes"?> <NewDataSet> <Category> <CategoryId>80</CategoryId>...
1
by: jaceyk | last post by:
Is it even remotely possible to update field names to the correct field name for the same table using a data definition query? We have a utility that spits out data in an Access database for use in...
4
by: justice750 | last post by:
Hi All, I am using a FormView control. The allows me to update records in the database. However, when a database field is null I can not update the field on the form. It works fine when the field...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.