سؤال

I am looking for a simple explanation of the difference between the pdo class and the pdostatement class.

For example, if I am accessing values in an instance of a new pdo object like $newObject = new PDO(connection info); Then how come I can also make a prepared statement in that same object by doing something like $newObject->prepare(prepared statement); $newObject->execute(statement); when those methods are part of the pdostatment object?

I am new to OOP so any simple explanation would really help.

هل كانت مفيدة؟

المحلول

The PDO class has the connection to the sql-server and returns PDOStatements, either executed or that can be executed later.

PDOStatements can either first be prepared and then executed, simply queried without preparing it (i.e. not binding any data to the statement).

After successfully executing a statement, the result can the be fetched from the PDOStatment with any of the "fetch" methods:

  • fetch
  • fetchAll

نصائح أخرى

You could see it like this:

PDO is the connection to your database, PDOStatement is your query.

Assume the following variables:

$pdo = new PDO($dsn, $username, $password);

$statement = $pdo->prepare($statement);

$pdo is an instance of PDO, whilst $statement is an instance of PDOStatement

Now on $statement you can call ->execute() to execute the query.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top