// create PDOstatement objectIn fact it should be exponentially faster than mysql_query + foreach even being a class.
$sth = $db->query('SELECT * FROM users ORDER BY u_id ASC');
// fetch all rows with it
$array = $sth->fetchAll();
print_r($array);
Register and join over 40,000 other developers!
Recent Topics
-
The Game You Are Waiting For?
WendellHarper - Dec 06 2020 01:21 PM
-
Quora and Reddit Backlinks
WendellHarper - Dec 06 2020 01:14 PM
-
Delete account
pindo - Jul 23 2020 01:33 AM
-
Print specific values from dictionary with a specific key name
Siten0308 - Jun 20 2019 01:43 PM
-
Learn algorithms and programming concepts
johnnylo - Apr 23 2019 07:49 AM
Recent Blog Entries
Recent Status Updates
Popular Tags
- networking
- Managed C++
- stream
- console
- database
- authentication
- Visual Basic 4 / 5 / 6
- session
- Connection
- asp.net
- import
- syntax
- hardware
- html5
- array
- mysql
- java
- php
- c++
- string
- C#
- html
- loop
- timer
- jquery
- ajax
- javascript
- programming
- android
- css
- assembly
- c
- form
- vb.net
- xml
- linked list
- login
- encryption
- pseudocode
- calculator
- sql
- python
- setup
- help
- game
- combobox
- binary
- hello world
- grid
- innerHTML

PDO: Database connections and abstraction
Started by Alexander, Oct 06 2010 11:32 AM
connection bound parameter
25 replies to this topic
#13
Posted 12 October 2010 - 09:55 PM
@DEViANT: You are thinking too complex again I think. This should return a complete associative array of the result set without need of a loop, using PDOStatement::fetchAll():
All new problems require investigation, and so if errors are problems, try to learn as much as you can and report back.
#14
Posted 13 October 2010 - 12:13 AM
@DEViANT: You are thinking too complex again I think. This should return a complete associative array of the result set without need of a loop, using PDOStatement::fetchAll():
// create PDOstatement objectIn fact it should be exponentially faster than mysql_query + foreach even being a class.
$sth = $db->query('SELECT * FROM users ORDER BY u_id ASC');
// fetch all rows with it
$array = $sth->fetchAll();
print_r($array);
I did try that yesterday, but I got the following error message :
Fatal error: Call to a member function fetchAll() on a non-object
#15
Posted 13 October 2010 - 10:53 AM
@DEViANT
Can you try your example within try/catch blocks, or see if $db->query returns FALSE?
PDO::query() returns a PDOStatement object, or FALSE on failure.
Can you try your example within try/catch blocks, or see if $db->query returns FALSE?
All new problems require investigation, and so if errors are problems, try to learn as much as you can and report back.
#16
Posted 14 October 2010 - 06:03 AM
well this is rather embarrassing
My SQL Query sellects 'users' instead of 'user'.
I got it working the way I want it, this is the code I used :
Brilliant tutorial, and I will most defenitely be using this from now on. Rep+!

My SQL Query sellects 'users' instead of 'user'.
I got it working the way I want it, this is the code I used :
// create PDOstatement object
$sth = $db->query('SELECT * FROM user ORDER BY u_id ASC');
// fetch all rows with it
$array = $sth->fetchAll(PDO::FETCH_ASSOC);
Brilliant tutorial, and I will most defenitely be using this from now on. Rep+!
#17
Posted 14 October 2010 - 07:40 AM
PDOstatement::numRows; This can get number of rows affected in an executed query statement much like PDOstatement::exec() return value;
Stumbled upon this though... In your original post you make a refference to "numRows". I tried to use it and found it doesnt work. A quick check on php.net told me its "rowCount"
#18
Posted 14 October 2010 - 03:18 PM
I am glad you had gotten it to work. I had updated that in my tutorial, thank you for pointing that out DEViANT!
All new problems require investigation, and so if errors are problems, try to learn as much as you can and report back.
#19
Posted 02 November 2010 - 03:42 PM
PDOstatement::rowCount; This can get number of rows affected in an executed query statement much like PDOstatement::exec() return value;
Does that mean I can do something like this?
if ($sth->execute() == 0) {
echo "No results found.";
}
EDIT: Oh, I missread, it wasn't execute it was exec. I solved it easily though.
$sth->execute()
if ($sth->rowCount() == 0) {
echo "No results found.";
}
#20
Posted 02 November 2010 - 04:05 PM
Does that mean I can do something like this?
Only PDO->exec returns row count (i.e. direct execution) you will need to check it separately:
$sth->execute();
if (!$sth->rowCount()) {
echo "No results found.";
}
All new problems require investigation, and so if errors are problems, try to learn as much as you can and report back.
#21
Posted 02 November 2010 - 04:06 PM
I already did that, thanks

#22
Posted 16 February 2011 - 06:08 PM
I was lacking enough knowledge to piece together all the bits and bytes on the web about using PDO for anything useful (transactions, sqlite), I have learned a lot by this. I will definitely be spamming you with questions when I run in to trouble!

#23
Posted 30 January 2012 - 11:24 AM
//PARAM_INT for int, PARAM_STR for string, PARAM_BOOL for bool
What would I use for enums? can't get it to work with either INT or STR.
#24
Posted 31 January 2012 - 10:45 PM
What would I use for enums? can't get it to work with either INT or STR.
How are you using it? According to MySQL however (MySQL :: MySQL 5.0 Reference Manual :: 10.4.4 The ENUM Type) :
An enumeration value must be a quoted string literal; it may not be an expression, even one that evaluates to a string value.
It may be entirely possible that it is not seen as a string yet, even with a PARAM_STR bound to it. I've never done that type before nor can I find any documentation specific to PDO.
Enums may be inefficient if they are modified down the road, maybe see this if it is possible to modify your database model: Why You Should Replace ENUM With Something Else | BrandonSavage.net or if required possibly sanitise with conventional methods and caution.
Alexander.
All new problems require investigation, and so if errors are problems, try to learn as much as you can and report back.
Also tagged with one or more of these keywords: connection, bound parameter
Language Forums →
Databases →
unable to read data from the transport connection an existing connection was forcibly closed by the remote host in postgresql...Started by viveq, 22 Aug 2013 ![]() |
|
![]() |
||
Language Forums →
PHP →
PHP Remote Server Database Connection ErrorStarted by KodeKool, 01 Mar 2013 ![]() |
|
![]() |
||
Language Forums →
Java →
NetBeans and Data Base connectionStarted by Petros, 16 Feb 2013 ![]() |
|
![]() |
||
Language Forums →
Other Languages →
ASP, ASP.NET and Coldfusion →
asp.net in my pc without internet connectionStarted by apysan, 28 Jan 2013 ![]() |
|
![]() |
||
Language Forums →
C and C++ →
Terminal and remote connectionsStarted by MaxBummer, 15 Jan 2013 ![]() |
|
![]() |
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download