/    Sign up×
Community /Pin to ProfileBookmark

fetchColumn() question

hello, according to [url]http://php.net/manual/en/pdostatement.fetchcolumn.php[/url]

[code]
<?php
$sth = $dbh->prepare(“SELECT name, colour FROM fruit”);
$sth->execute();

print(“Fetch the first column from the first row in the result set:n”);
$result = $sth->fetchColumn();
print(“name = $resultn”);

print(“Fetch the second column from the second row in the result set:n”);
$result = $sth->fetchColumn(1);
print(“colour = $resultn”);
?>
[/code]

I tried to do the same

[code]

$sth = $conn->prepare(“SELECT col_1, col_2 FROM my_tb”);
$sth->execute();

print(“Fetch the first column from the first row in the result set:n”);
$result = $sth->fetchColumn();
print(“col_1 = $resultn”);

print(“Fetch the second column from the second row in the result set:n”);
$result = $sth->fetchColumn(1);
print(“col_2 = $resultn”);

[/code]

Somehow, only the first column col_1 shows up. Did I do something wrong?

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@NogDogFeb 06.2017 — Are you sure you got more than one result row?
[code=php]
$sth = $conn->prepare("SELECT col_1, col_2 FROM my_tb");
$sth->execute();

print("Fetch the first column from the first row in the result set:n");
$result = $sth->fetchColumn();
if($result !== false) {
print("col_1 = $resultn");
print("Fetch the second column from the second row in the result set:n");
$result = $sth->fetchColumn(1);
if($result !== false) {
print("col_2 = $resultn");
}
else {
print("Only one row returned.n");
}
}
else {
print("No rows returned.n");
}[/code]
Copy linkTweet thisAlerts:
@rich1812authorFeb 06.2017 — Ah duh! I missed the fine print on the second print("Fetch the second column from the second row in the result set:n");

Is this what it means in $result = $sth->fetchColumn(0) it means column in row 0 $result = $sth->fetchColumn(1) column in row 1...

I thougt it means column in coumn 0, column1 ...

So inorder to do this col_1, col_2 on the same row, I would need to search that row first before I go onto the next row?
Copy linkTweet thisAlerts:
@NogDogFeb 06.2017 — Not exactly, it means fetch the next row (which is the first row the first time it's called), and grab the specified column (defaulting to column 0 -- the first column). So the next time you call it, it retrieves the next available result row if there is one, just like a fetch() call does. Therefore, fetchColumn() is really only useful if you are only concerned with one column. Otherwise you'll want to use a fetch().
×

Success!

Help @rich1812 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.27,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...