pdo dbquery with pdo dbfetch

compare/contrast the pdo dbquery with the pdo dbfetch
LVL 1
rgb192Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Julian HansenCommented:
Assume you are refering to ::query and ::fetch

The PDO::query method executes a query and returns a PDOStatement object
http://www.php.net/manual/en/pdo.query.php

PDOStatement::fetch - fetches the next row from the result.
http://php.net/manual/en/pdostatement.fetch.php

The PDO library is a bit different from the mysql flavours. The latter use a returned result to execute subsequent calls whereas PDO returns an object (PDOStatement) which is then used to iterate through the results.
0
Ray PaseurCommented:
With MySQL and MySQLi a query returns a resource or object, respectively.  You use the resource or object to retrieve the rows of the result set.
// MYSQL ASSUMES PROCEDURAL CONNECTION
$sql = "SELECT 1+1"
$res = mysql_query($sql);
$row = mysql_fetch_object($res);
var_dump($row);

Open in new window

// MYSQLi ASSUMES $mysqli CONNECTION
$sql = "SELECT 1+1"
$res = $mysqli->query($sql);
$row = $res->fetch_object($res);
var_dump($row);

Open in new window

Compare these to PDO Example 1

Compare and contrast these:
http://php.net/manual/en/pdostatement.fetch.php
http://php.net/manual/en/pdostatement.fetchall.php

With fetchall() you have a risk of transferring a very, very large results data set if you do not use appropriate LIMIT clauses in your queries.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rgb192Author Commented:
The PDO library is a bit different from the mysql flavours. The latter use a returned result to execute subsequent calls whereas PDO returns an object (PDOStatement) which is then used to iterate through the results.


could you explain without using code? such as real world example





With MySQL and MySQLi a query returns a resource or object, respectively.  You use the resource or object to retrieve the rows of the result set.

what does pdo return
0
PMI ACP® Project Management

Prepare for the PMI Agile Certified Practitioner (PMI-ACP)® exam, which formally recognizes your knowledge of agile principles and your skill with agile techniques.

Julian HansenCommented:
There was an error in my original post - MySQLi does not use the same mechanism as MySQL.

mysql - returns a resource that is used in subsequent calls i.e.

$result = mysql_query($query);
$row - mysql_fetch_assoc($result);

Open in new window


PDO and MySQLI - return an object which is then used to operate on the results

MySQLi
$result = $mysqli->query($query);
// $result is now an object rather than a resource identifier
// We use the returned object to execute a fetch on the recordset
$row = $result->fetch_assoc();

Open in new window

PDO
$statement = $pdo->prepare($query, $parameters);
$statement->execute();
$row = $statement->fetch();

Open in new window


This is oversimplifying it because MySQLi and PDO bring a lot more to the party - like prepared statments. You would do well to read the manual pages for these libraries to gain a full understanding of what is required.

Which to use (http://net.tutsplus.com/tutorials/php/pdo-vs-mysqli-which-should-you-use/)

MySQLi might be a little easier to code - PDO has more options.
0
Ray PaseurCommented:
what does pdo return
Please give yourself a little time with this article.  PDO is a system of APIs and classes that allows PHP to talk to data base engines.  It's comprehensible, but it takes a while to learn it all.  The article has links to the important man pages that you want to study.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/A_11177-PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html
0
rgb192Author Commented:
mysql returns resource

mysqli,pdo returns object
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.