You are here

function DB_common::prepare in Flickr API 5

Prepares a query for multiple execution with execute()

Creates a query that can be run multiple times. Each time it is run, the placeholders, if any, will be replaced by the contents of execute()'s $data argument.

Three types of placeholders can be used: + <kbd>?</kbd> scalar value (i.e. strings, integers). The system will automatically quote and escape the data. + <kbd>!</kbd> value is inserted 'as is' + <kbd>&</kbd> requires a file name. The file's contents get inserted into the query (i.e. saving binary data in a db)

Example 1. <code> $sth = $db->prepare('INSERT INTO tbl (a, b, c) VALUES (?, !, &)'); $data = array( "John's text", "'it''s good'", 'filename.txt' ); $res = $db->execute($sth, $data); </code>

Use backslashes to escape placeholder characters if you don't want them to be interpreted as placeholders: <pre> "UPDATE foo SET col=? WHERE col='over \& under'" </pre>

With some database backends, this is emulated.

{@internal ibase and oci8 have their own prepare() methods.}}

Parameters

string $query the query to be prepared:

Return value

mixed DB statement resource on success. A DB_Error object on failure.

See also

DB_common::execute()

7 calls to DB_common::prepare()
DB_common::autoPrepare in phpFlickr/PEAR/DB/common.php
Automaticaly generates an insert or update query and pass it to prepare()
DB_common::getAll in phpFlickr/PEAR/DB/common.php
Fetches all of the rows from a query result
DB_common::getAssoc in phpFlickr/PEAR/DB/common.php
Fetches an entire query result and returns it as an associative array using the first column as the key
DB_common::getCol in phpFlickr/PEAR/DB/common.php
Fetches a single column from a query result and returns it as an indexed array
DB_common::getOne in phpFlickr/PEAR/DB/common.php
Fetches the first column of the first row from a query result

... See full list

File

phpFlickr/PEAR/DB/common.php, line 747

Class

DB_common
DB_common is the base class from which each database driver class extends

Code

function prepare($query) {
  $tokens = preg_split('/((?<!\\\\)[&?!])/', $query, -1, PREG_SPLIT_DELIM_CAPTURE);
  $token = 0;
  $types = array();
  $newtokens = array();
  foreach ($tokens as $val) {
    switch ($val) {
      case '?':
        $types[$token++] = DB_PARAM_SCALAR;
        break;
      case '&':
        $types[$token++] = DB_PARAM_OPAQUE;
        break;
      case '!':
        $types[$token++] = DB_PARAM_MISC;
        break;
      default:
        $newtokens[] = preg_replace('/\\\\([&?!])/', "\\1", $val);
    }
  }
  $this->prepare_tokens[] =& $newtokens;
  end($this->prepare_tokens);
  $k = key($this->prepare_tokens);
  $this->prepare_types[$k] = $types;
  $this->prepared_queries[$k] = implode(' ', $newtokens);
  return $k;
}