/** * Add 1000 user records in dummy database, WITH prepared statement. * * * @return */ function withStatementTest() { $db = coreContext::getInstance()->getDatabase(); $stmt = new coreDatabaseStatementMySQL($db, "INSERT users (username, userlevel, joindate) VALUES (?, ?, NOW())"); $db->query("LOCK TABLES users WRITE"); for ($i = 0; $i < 1000; $i++) { $s = 'framenum' . $i; $level = $i & 0xf; $stmt->execute(array($s, $level)); } $db->query("UNLOCK TABLES"); }
/** * Delete a set of flashcards. * * @param int $userId * @param array $cardIds Array of flashcard ids (kanji framenum or unicode value) * * @return array Returns an array of succesfully deleted flashcard ids or false */ public static function deleteFlashcards($userId, $cards) { $tableName = self::getInstance()->getName(); // lock the table (to speedup index) (minimal speed gain..) self::$db->query('LOCK TABLE ' . $tableName . ' WRITE'); // prepare statement and execute for all cards $stmt = new coreDatabaseStatementMySQL(self::$db, sprintf('DELETE FROM %s WHERE userid = %d AND framenum = ?', $tableName, $userId)); try { $done = array(); foreach ($cards as $id) { if (!$stmt->execute(array($id))) { break; } if ($stmt->rowCount() > 0) { $done[] = $id; } } } catch (coreException $e) { $done = false; } // unlock table self::$db->query('UNLOCK TABLES'); // return succesfully added ids return $done; }