PDO returning incorrect, but duplicate, data. Key's not in database.
I'm new to using $pdo statements so might be something simple I haven't yet read on php.net. I'm receiving duplicate results when querying the database.
Result:
[0] => Array
(
[umeta_id] => 31
[0] => 31
[user_id] => 2
[1] => 2
[meta_key] => fbmeta
[2] => fbmeta
[meta_value] => someMetaValueStuff;
[3] => someMetaValueStuff;
)
The query is quite simple:
function getData()
global $pdo;
$query = $pdo->prepare('SELECT * FROM usermeta WHERE meta_key = "fbmeta" LIMIT 0,30');
$query->execute();
return $query->fetchAll();
print_r( getData() );
The problem is that the named keys (umeta_id
, user_id
, meta_key
, meta_value
) DO exist, the numeric keys do not. How come the query returns these? And how do I prevent them from even being returned?
php pdo
add a comment |
I'm new to using $pdo statements so might be something simple I haven't yet read on php.net. I'm receiving duplicate results when querying the database.
Result:
[0] => Array
(
[umeta_id] => 31
[0] => 31
[user_id] => 2
[1] => 2
[meta_key] => fbmeta
[2] => fbmeta
[meta_value] => someMetaValueStuff;
[3] => someMetaValueStuff;
)
The query is quite simple:
function getData()
global $pdo;
$query = $pdo->prepare('SELECT * FROM usermeta WHERE meta_key = "fbmeta" LIMIT 0,30');
$query->execute();
return $query->fetchAll();
print_r( getData() );
The problem is that the named keys (umeta_id
, user_id
, meta_key
, meta_value
) DO exist, the numeric keys do not. How come the query returns these? And how do I prevent them from even being returned?
php pdo
before anyone asks, yes I have manually checked and limited the database to a small collection, so the duplicates really do not exist.
– rkeet
Apr 29 '13 at 9:12
add a comment |
I'm new to using $pdo statements so might be something simple I haven't yet read on php.net. I'm receiving duplicate results when querying the database.
Result:
[0] => Array
(
[umeta_id] => 31
[0] => 31
[user_id] => 2
[1] => 2
[meta_key] => fbmeta
[2] => fbmeta
[meta_value] => someMetaValueStuff;
[3] => someMetaValueStuff;
)
The query is quite simple:
function getData()
global $pdo;
$query = $pdo->prepare('SELECT * FROM usermeta WHERE meta_key = "fbmeta" LIMIT 0,30');
$query->execute();
return $query->fetchAll();
print_r( getData() );
The problem is that the named keys (umeta_id
, user_id
, meta_key
, meta_value
) DO exist, the numeric keys do not. How come the query returns these? And how do I prevent them from even being returned?
php pdo
I'm new to using $pdo statements so might be something simple I haven't yet read on php.net. I'm receiving duplicate results when querying the database.
Result:
[0] => Array
(
[umeta_id] => 31
[0] => 31
[user_id] => 2
[1] => 2
[meta_key] => fbmeta
[2] => fbmeta
[meta_value] => someMetaValueStuff;
[3] => someMetaValueStuff;
)
The query is quite simple:
function getData()
global $pdo;
$query = $pdo->prepare('SELECT * FROM usermeta WHERE meta_key = "fbmeta" LIMIT 0,30');
$query->execute();
return $query->fetchAll();
print_r( getData() );
The problem is that the named keys (umeta_id
, user_id
, meta_key
, meta_value
) DO exist, the numeric keys do not. How come the query returns these? And how do I prevent them from even being returned?
php pdo
php pdo
asked Apr 29 '13 at 9:11
rkeetrkeet
1,89621735
1,89621735
before anyone asks, yes I have manually checked and limited the database to a small collection, so the duplicates really do not exist.
– rkeet
Apr 29 '13 at 9:12
add a comment |
before anyone asks, yes I have manually checked and limited the database to a small collection, so the duplicates really do not exist.
– rkeet
Apr 29 '13 at 9:12
before anyone asks, yes I have manually checked and limited the database to a small collection, so the duplicates really do not exist.
– rkeet
Apr 29 '13 at 9:12
before anyone asks, yes I have manually checked and limited the database to a small collection, so the duplicates really do not exist.
– rkeet
Apr 29 '13 at 9:12
add a comment |
3 Answers
3
active
oldest
votes
It's not duplicates, it's just the current FETCH_MODE
you're using. To get as associative keys only you need to specify as such; by default it fetches as both.
Use like so:
$query->fetchAll(PDO::FETCH_NUM); // to fetch with numeric indexes
$query->fetchAll(PDO::FETCH_ASSOC); // to fetch with associative indexes
fetchAll docs
fetch docs
2
Cool that fixed it. Too bad I cant select 2 answers as answering, both you and Yogesh were at it at the same time.
– rkeet
Apr 29 '13 at 9:18
@Nukeface I think its my first...:). But you can accept anyone's answer, its your choice...:)
– Yogesh Suthar
Apr 29 '13 at 9:20
1
Will do when the timer counts down, you guys were quick ;) Will choose this one though, the fetch docs went to the fetch function where I should've noticed the differences on my first look-through, and didn't.
– rkeet
Apr 29 '13 at 9:21
add a comment |
This is not duplicate data fetchAll
returns data in numeric array as well as associative array.
See the Docs
Use this for retrieving only associative array
return $query->fetchAll(PDO::FETCH_ASSOC);
add a comment |
seems to do the trick for me. Put this on top
use IlluminateDatabaseEventsStatementPrepared;
when ever fetching include pdo::fetch_assoc
return($pdo->fetchAll(PDO::FETCH_ASSOC));
Seems your using this: github.com/illuminate/database This does not solve the original issue, as this is not just plain PHP but an externally required package. Required either by your project, some framework or cms or another package dependency.
– rkeet
Nov 14 '18 at 7:30
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f16274963%2fpdo-returning-incorrect-but-duplicate-data-keys-not-in-database%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
It's not duplicates, it's just the current FETCH_MODE
you're using. To get as associative keys only you need to specify as such; by default it fetches as both.
Use like so:
$query->fetchAll(PDO::FETCH_NUM); // to fetch with numeric indexes
$query->fetchAll(PDO::FETCH_ASSOC); // to fetch with associative indexes
fetchAll docs
fetch docs
2
Cool that fixed it. Too bad I cant select 2 answers as answering, both you and Yogesh were at it at the same time.
– rkeet
Apr 29 '13 at 9:18
@Nukeface I think its my first...:). But you can accept anyone's answer, its your choice...:)
– Yogesh Suthar
Apr 29 '13 at 9:20
1
Will do when the timer counts down, you guys were quick ;) Will choose this one though, the fetch docs went to the fetch function where I should've noticed the differences on my first look-through, and didn't.
– rkeet
Apr 29 '13 at 9:21
add a comment |
It's not duplicates, it's just the current FETCH_MODE
you're using. To get as associative keys only you need to specify as such; by default it fetches as both.
Use like so:
$query->fetchAll(PDO::FETCH_NUM); // to fetch with numeric indexes
$query->fetchAll(PDO::FETCH_ASSOC); // to fetch with associative indexes
fetchAll docs
fetch docs
2
Cool that fixed it. Too bad I cant select 2 answers as answering, both you and Yogesh were at it at the same time.
– rkeet
Apr 29 '13 at 9:18
@Nukeface I think its my first...:). But you can accept anyone's answer, its your choice...:)
– Yogesh Suthar
Apr 29 '13 at 9:20
1
Will do when the timer counts down, you guys were quick ;) Will choose this one though, the fetch docs went to the fetch function where I should've noticed the differences on my first look-through, and didn't.
– rkeet
Apr 29 '13 at 9:21
add a comment |
It's not duplicates, it's just the current FETCH_MODE
you're using. To get as associative keys only you need to specify as such; by default it fetches as both.
Use like so:
$query->fetchAll(PDO::FETCH_NUM); // to fetch with numeric indexes
$query->fetchAll(PDO::FETCH_ASSOC); // to fetch with associative indexes
fetchAll docs
fetch docs
It's not duplicates, it's just the current FETCH_MODE
you're using. To get as associative keys only you need to specify as such; by default it fetches as both.
Use like so:
$query->fetchAll(PDO::FETCH_NUM); // to fetch with numeric indexes
$query->fetchAll(PDO::FETCH_ASSOC); // to fetch with associative indexes
fetchAll docs
fetch docs
edited May 29 '16 at 16:58
answered Apr 29 '13 at 9:14
Emmanuel OkekeEmmanuel Okeke
1,192812
1,192812
2
Cool that fixed it. Too bad I cant select 2 answers as answering, both you and Yogesh were at it at the same time.
– rkeet
Apr 29 '13 at 9:18
@Nukeface I think its my first...:). But you can accept anyone's answer, its your choice...:)
– Yogesh Suthar
Apr 29 '13 at 9:20
1
Will do when the timer counts down, you guys were quick ;) Will choose this one though, the fetch docs went to the fetch function where I should've noticed the differences on my first look-through, and didn't.
– rkeet
Apr 29 '13 at 9:21
add a comment |
2
Cool that fixed it. Too bad I cant select 2 answers as answering, both you and Yogesh were at it at the same time.
– rkeet
Apr 29 '13 at 9:18
@Nukeface I think its my first...:). But you can accept anyone's answer, its your choice...:)
– Yogesh Suthar
Apr 29 '13 at 9:20
1
Will do when the timer counts down, you guys were quick ;) Will choose this one though, the fetch docs went to the fetch function where I should've noticed the differences on my first look-through, and didn't.
– rkeet
Apr 29 '13 at 9:21
2
2
Cool that fixed it. Too bad I cant select 2 answers as answering, both you and Yogesh were at it at the same time.
– rkeet
Apr 29 '13 at 9:18
Cool that fixed it. Too bad I cant select 2 answers as answering, both you and Yogesh were at it at the same time.
– rkeet
Apr 29 '13 at 9:18
@Nukeface I think its my first...:). But you can accept anyone's answer, its your choice...:)
– Yogesh Suthar
Apr 29 '13 at 9:20
@Nukeface I think its my first...:). But you can accept anyone's answer, its your choice...:)
– Yogesh Suthar
Apr 29 '13 at 9:20
1
1
Will do when the timer counts down, you guys were quick ;) Will choose this one though, the fetch docs went to the fetch function where I should've noticed the differences on my first look-through, and didn't.
– rkeet
Apr 29 '13 at 9:21
Will do when the timer counts down, you guys were quick ;) Will choose this one though, the fetch docs went to the fetch function where I should've noticed the differences on my first look-through, and didn't.
– rkeet
Apr 29 '13 at 9:21
add a comment |
This is not duplicate data fetchAll
returns data in numeric array as well as associative array.
See the Docs
Use this for retrieving only associative array
return $query->fetchAll(PDO::FETCH_ASSOC);
add a comment |
This is not duplicate data fetchAll
returns data in numeric array as well as associative array.
See the Docs
Use this for retrieving only associative array
return $query->fetchAll(PDO::FETCH_ASSOC);
add a comment |
This is not duplicate data fetchAll
returns data in numeric array as well as associative array.
See the Docs
Use this for retrieving only associative array
return $query->fetchAll(PDO::FETCH_ASSOC);
This is not duplicate data fetchAll
returns data in numeric array as well as associative array.
See the Docs
Use this for retrieving only associative array
return $query->fetchAll(PDO::FETCH_ASSOC);
answered Apr 29 '13 at 9:13
Yogesh SutharYogesh Suthar
26.8k175891
26.8k175891
add a comment |
add a comment |
seems to do the trick for me. Put this on top
use IlluminateDatabaseEventsStatementPrepared;
when ever fetching include pdo::fetch_assoc
return($pdo->fetchAll(PDO::FETCH_ASSOC));
Seems your using this: github.com/illuminate/database This does not solve the original issue, as this is not just plain PHP but an externally required package. Required either by your project, some framework or cms or another package dependency.
– rkeet
Nov 14 '18 at 7:30
add a comment |
seems to do the trick for me. Put this on top
use IlluminateDatabaseEventsStatementPrepared;
when ever fetching include pdo::fetch_assoc
return($pdo->fetchAll(PDO::FETCH_ASSOC));
Seems your using this: github.com/illuminate/database This does not solve the original issue, as this is not just plain PHP but an externally required package. Required either by your project, some framework or cms or another package dependency.
– rkeet
Nov 14 '18 at 7:30
add a comment |
seems to do the trick for me. Put this on top
use IlluminateDatabaseEventsStatementPrepared;
when ever fetching include pdo::fetch_assoc
return($pdo->fetchAll(PDO::FETCH_ASSOC));
seems to do the trick for me. Put this on top
use IlluminateDatabaseEventsStatementPrepared;
when ever fetching include pdo::fetch_assoc
return($pdo->fetchAll(PDO::FETCH_ASSOC));
answered Nov 14 '18 at 3:01
gritgrit
9
9
Seems your using this: github.com/illuminate/database This does not solve the original issue, as this is not just plain PHP but an externally required package. Required either by your project, some framework or cms or another package dependency.
– rkeet
Nov 14 '18 at 7:30
add a comment |
Seems your using this: github.com/illuminate/database This does not solve the original issue, as this is not just plain PHP but an externally required package. Required either by your project, some framework or cms or another package dependency.
– rkeet
Nov 14 '18 at 7:30
Seems your using this: github.com/illuminate/database This does not solve the original issue, as this is not just plain PHP but an externally required package. Required either by your project, some framework or cms or another package dependency.
– rkeet
Nov 14 '18 at 7:30
Seems your using this: github.com/illuminate/database This does not solve the original issue, as this is not just plain PHP but an externally required package. Required either by your project, some framework or cms or another package dependency.
– rkeet
Nov 14 '18 at 7:30
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f16274963%2fpdo-returning-incorrect-but-duplicate-data-keys-not-in-database%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
before anyone asks, yes I have manually checked and limited the database to a small collection, so the duplicates really do not exist.
– rkeet
Apr 29 '13 at 9:12