public static function getRow(\Pdo $pdo, array $columns, string $tableName, array $conditions) { $query = self::baseQuery($columns, $tableName); $query .= DefaultFunctions::addConditionsToQuery($conditions); $query .= ' LIMIT 1'; return self::executeFetchStatement($pdo->prepare($query)); }
/** * @override */ public function prepare($statement, $options = array()) { $result = parent::prepare($statement, $options); if ($result instanceof \PDOStatement) { return new PdoStatement($this, $result); } return $result; }
/** * Prepares the PDO statements */ protected function prepareStatements() { if (!empty($this->statements)) { return; } $this->statements['load'] = $this->db->prepare('SELECT * FROM `' . $this->options['table'] . '` WHERE `userId` = :userId ' . 'AND `scope` = :scope AND `name` = :name AND `args` = :args'); $this->statements['save'] = $this->db->prepare('INSERT INTO `' . $this->options['table'] . '` (`userId`, `scope`, `name`, `args`, `cachedUntil`, `xml`) VALUES ' . '(:userId, :scope, :name, :args, :cachedUntil, :xml)'); $this->statements['delete'] = $this->db->prepare('DELETE FROM`' . $this->options['table'] . '` WHERE `userId` = :userId ' . 'AND `scope` = :scope AND `name` = :name AND `args` = :args'); }
function it_finds_multiple_records_by_name(\Pdo $connection, \PDOStatement $sqlResult) { $searchForName = "oso"; $expectedObject = array(new StringRecord(1, $searchForName), new StringRecord(1, $searchForName)); $connection->prepare("SELECT * FROM Testtable WHERE `name` = :name;")->willReturn($sqlResult); $sqlResult->execute(array('name' => $searchForName))->shouldBeCalled(); $sqlResult->fetchObject("\\spec\\MusicAnt\\DataSource\\StringRecord")->willReturn($expectedObject); $this->find(array('name' => $searchForName))->shouldReturn($expectedObject); }
public function setup() { $pdo = new \Pdo($GLOBALS['DB_DSN'], $GLOBALS['DB_USER'], $GLOBALS['DB_PASSWD'], array(\PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true, \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION, \PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC)); $sql = "\n DROP TABLE IF EXISTS `entity`;\n CREATE TABLE `entity` (\n `id` int(11) unsigned NOT NULL AUTO_INCREMENT,\n `name` varchar(225) DEFAULT NULL,\n PRIMARY KEY (`id`)\n );\n "; $stmt = $pdo->prepare($sql); $stmt->execute(); require_once 'Mikron.php'; $this->mikron = new Mikron($pdo); }
/** * Delete data * @param [id] [data's id] */ public function delete($id) { $sql = "DELETE FROM " . $this->table . " WHERE id = :id"; try { $delete = $this->conn->prepare($sql); $delete->bindValue(':id', $id, \PDO::PARAM_INT); if ($delete->execute()) { return true; } } catch (PDOexception $e) { return false; } }
/** * Get table checksum * @return mixed */ private function getTableChecksum() { $sql = "CHECKSUM TABLE items"; $q = $this->db->prepare($sql); $q->execute(); $rows = $q->fetchAll(); if (is_array($rows) && isset($rows[0]['Checksum'])) { $checksum = (int) $rows[0]['Checksum']; } else { $checksum = null; } return $checksum; }
/** * {@inheritdoc} */ public function restore($dumpTargetFile, array $ignoreTables = []) { $connection = new Pdo("mysql:host={$this->host};port=3306", $this->username, $this->password); $connection->query("USE {$this->database}"); foreach ($connection->query('SHOW TABLES')->fetchAll(PDO::FETCH_ASSOC) as $table) { $table = array_pop($table); if (in_array($table, $ignoreTables)) { continue; } $connection->query("DROP TABLE `{$table}`")->execute(); } $sql = file_get_contents($dumpTargetFile); $connection->prepare($sql)->execute(); }
static function query($sql, $data = array()) { $rst = self::$inst->prepare($sql); foreach ($data as $key => &$value) { $rst->bindParam($key, $value, is_numeric($value) ? \PDO::PARAM_INT : \PDO::PARAM_STR); } $rst->setFetchMode(\PDO::FETCH_ASSOC); $rst->execute(); $errorInfo = $rst->errorInfo(); if ($errorInfo[2]) { throw new \Exception($errorInfo[2]); } return $rst; }
/** * 执行sql语句 * @param string $sql sql语句 * @param array $params 参数 * @throws \PDOException */ public function query($sql, array $params = array()) { if (defined('DEBUG_SQL')) { // 输出调试的sql语句 echo "<pre>placeholder sql: {$sql}<hr/>"; print_r($params); foreach ($params as $key => $placeholder) { // 字符串加上引号 is_string($placeholder) and $placeholder = "'{$placeholder}'"; $start = strpos($sql, $key); $end = strlen($key); $sql = substr_replace($sql, $placeholder, $start, $end); } exit("<hr/> origin sql: {$sql}</pre>"); } // 预处理语句 $this->stmt = $this->pdo->prepare($sql); // 参数绑定 $this->bindValue($params); // sql语句执行 $this->stmt->execute(); // 结果解析成数组 $this->stmt->setFetchMode(\PDO::FETCH_ASSOC); }
private function execute($statement, $bind_parameters = [], $ret = FALSE) { $stmt = parent::prepare($statement); $_bind_parameters = []; foreach ($bind_parameters as $key => $value) { if (TRUE === is_array($value)) { $_bind_parameters[$key] = $value[0]; } else { $_bind_parameters[$key] = $value; } } $result = $stmt->execute($_bind_parameters); if (TRUE === $ret) { $stmt->closeCursor(); return $result; } else { return $stmt; } }
/** * Modifie une ligne en fonction d'un identifiant * @param array $data Un tableau associatif de valeurs à insérer * @param mixed $id L'identifiant de$ la ligne à modifier * @param boolean $stripTags Active le strip_tags automatique sur toutes les valeurs * @return mixed La valeur de retour de la méthode execute() */ public function update(array $data, $id, $stripTags = true) { if (!is_numeric($id)) { return false; } $sql = "UPDATE " . $this->table . " SET "; foreach ($data as $key => $value) { $sql .= "{$key} = :{$key}, "; } $sql = substr($sql, 0, -2); $sql .= " WHERE id = :id"; $sth = $this->dbh->prepare($sql); foreach ($data as $key => $value) { $value = $stripTags ? strip_tags($value) : $value; $sth->bindValue(":" . $key, $value); } $sth->bindValue(":id", $id); return $sth->execute(); }
*/ $getRecent = function ($db) { $stmt = $db->query('SELECT paste_id, name, added FROM paste ORDER BY added DESC LIMIT 5 OFFSET 0'); $stmt->execute(); $recent = $stmt->fetchAll(); return $recent !== false ? $recent : array(); }; /** * Gets the details of a paste from the database. * * @param PDO $db * @param string $id * @return array */ $getPaste = function ($db, $id) { $stmt = $db->prepare('SELECT * FROM paste WHERE paste_id = :id'); $stmt->execute(array('id' => $id)); return $stmt->fetch(); }; /** * Build the application, and set-up twig integration. */ $app = new Silex\Application(); $app->register(new Silex\Provider\TwigServiceProvider(), array('twig.path' => __DIR__ . '/../views')); $app['twig'] = $app->share($app->extend('twig', function ($twig, $app) use($config) { if (isset($config['debug']) && $config['debug'] == true) { $twig->addFilter('var_dump', new \Twig_Filter_Function('var_dump')); $app['debug'] = true; } $twig->addFilter('highlight', new \Twig_Filter_Function('highlightSaffire')); return $twig;
<?php $db = new Pdo(''); $buys = array(); $buycds = array(); $ids = array(); $sth = $db->prepare('select * from buy'); $sth->execute(array()); while ($line = $sth->fetch()) { $line['buy_items'] = array(); $buys[$line['buycd']] = $line; $buycds[$line['buycd']] = $line['buycd']; $ids[$line['id']] = $line['id']; } $sth->closeCursor(); $itemcds = array(); if (count($buycds)) { $sth = $db->prepare('select * from buy_item where buycd in (' . implode(',', $buycds) . ')'); $sth->execute(array()); while ($line = $sth->fetch()) { $buys[$line['buycd']]['buy_items'][$line['seq']] = $line; $itemcds[$line['itemcd']] = $line['itemcd']; } $sth->closeCursor(); } $items = array(); if (count($itemcds)) { $sth = $db->prepare('select * from item where itemcd in (' . implode(',', $itemcds) . ')'); $sth->execute(array()); while ($line = $sth->fetch()) { $items[$line['itemcd']] = $line;
WHERE c_d.language_id = 2 ORDER BY c_p1.level, c.category_id '); $sth->execute(); $categories = []; while ($row = $sth->fetch(\PDO::FETCH_ASSOC)) { $categories[$row['category_id']] = $row; } foreach ($categories as &$category) { echo $category['category_id'] . " (" . $category['path'] . ")\n"; $sth = $dst->prepare(' INSERT INTO category SET `parent_id` = :parent_id, `top` = 1, `column` = 1, `sort_order` = :sort_order, `status` = 1, `date_added` = now(), `date_modified` = now(), `image` = :image '); $parentId = 0; if ($category['parent_id']) { if (!isset($categories[$category['parent_id']])) { throw new \Exception(sprintf('Parent category %d not found', $category['parent_id'])); } $categoryParent = $categories[$category['parent_id']]; if (!isset($categoryParent['dst_category_id'])) { throw new \Exception(sprintf('Parent category %d not saved yet', $category['parent_id'])); } $parentId = $categoryParent['dst_category_id'];
public static function tearDownAfterClass() { $sth = self::$pdo->prepare("DROP TABLE IF EXISTS users"); $sth->execute(); }
/** * Default speaker URI * * Change this value to specify a different default speaker URI when a speaker does not have an entry on joind.in. */ $defaultSpeakerUri = 'http://www.zendcon.com'; /** * Default speaker twitter username * * Change this value to specify a different default speaker twitter username when a speaker does not have an entry on joind.in. */ $defaultSpeakerTwitter = 'zendcon'; $dbPath = realpath(__DIR__ . '/../data/db/conference.db'); $pdo = new Pdo('sqlite:' . $dbPath); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $insertSpeaker = $pdo->prepare('INSERT INTO speakers (id, name, url, twitter) VALUES (:id, :name, :url, :twitter)'); $insertTalk = $pdo->prepare('INSERT INTO talks (id, title, abstract, day, start_time) VALUES (:id, :title, :abstract, :day, :start_time)'); $insertLink = $pdo->prepare('INSERT INTO talks_speakers (talk_id, speaker_id) VALUES (:talk_id, :speaker_id)'); $client = new Client(); $client->setOptions(['adapter' => Curl::class]); $request = $client->getRequest(); $headers = $request->getHeaders(); $headers->addHeaderLine('Accept', 'application/json'); $client->setUri(sprintf('http://api.joind.in/v2.1/events/%d/talks?verbose=yes&resultsperpage=0', $conferenceId)); $client->setMethod('GET'); printf("Fetching talks for ZendCon 2015..."); $response = $client->send(); $json = $response->getBody(); $payload = json_decode($json); $talks = $payload->talks; printf("[DONE]\n");