public function testPExecuteQuery() { $rs = $this->db->PExecute('SELECT * From Country WHERE Continent = ?', 'Africa'); $this->assertTrue($rs); $this->assertEquals($this->db->getnumOfRows(), 58); $this->assertEquals($this->db->getnumOfRowsAffected(), 58); $this->assertFalse($this->db->PExecute('SELECT * From Country WHERE Continent = ?', 'naranjas')); }
/** * delSessionRef - delete sessions containing a specific reference * * @param string $ref * @return boolean */ public function delSessionRef($ref) { return $this->DB->PExecute('DELETE FROM ' . $this->dalmp_sessions_table . ' WHERE ref=?', $ref); }
$db->StartTrans(); $credit = $db->PGetOne('SELECT credit FROM t_test2 WHERE id=? FOR UPDATE', 1); if ($credit > 0) { $db->PExecute('UPDATE t_test2 SET credit=credit - ? WHERE id = ?', 100, 1); } echo "process {$i} credit: ", $db->PGetOne('SELECT credit FROM t_test2'), PHP_EOL; $rs = $db->CompleteTrans(); echo 'Transaction returned: ', (bool) $rs, PHP_EOL; exit($i); break; case 3: echo "In process: 3", PHP_EOL; $db = new DALMP\Database("utf8://{$user}:{$password}@{$host}:{$port}/dalmp"); $db->StartTrans(); $credit = $db->PGetOne('SELECT credit FROM t_test2 WHERE id=? FOR UPDATE', 1); if ($credit > 0) { $db->PExecute('UPDATE t_test2 SET credit=credit - ? WHERE id = ?', 100, 1); } echo "process {$i} credit: ", $db->PGetOne('SELECT credit FROM t_test2'), PHP_EOL; $rs = $db->CompleteTrans(); echo 'Transaction returned: ', (bool) $rs, PHP_EOL; exit($i); } } } while (pcntl_waitpid(0, $status) != -1) { $status = pcntl_wexitstatus($status); echo "Child {$status} completed", PHP_EOL; } # ------------------------------------------------------------------------------ echo PHP_EOL, str_repeat('-', 80), PHP_EOL, 'Time: ', $timer->getPageLoadTime(), ' - Memory: ', $timer->getMemoryUsage(1), PHP_EOL, str_repeat('-', 80), PHP_EOL;
<?php require_once '../../MPLT.php'; $timer = new MPLT(); require_once '../../src/dalmp.php'; # ------------------------------------------------------------------------------ $user = getenv('MYSQL_USER') ?: 'root'; $password = getenv('MYSQL_PASS') ?: ''; $host = getenv('MYSQL_HOST') ?: '127.0.0.1'; $port = getenv('MYSQL_PORT') ?: '3306'; define('DALMP_MYSQLI_INIT_COMMAND', 'SET time_zone="-05:00"'); $db = new DALMP\Database("utf8://{$user}:{$password}@{$host}:{$port}/dalmp"); echo "-05:00 time: ", $db->GetOne('SELECT NOW()'); /** * load zone files to mysql * mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root mysql */ $db->PExecute('SET time_zone=?', '+00:00'); echo PHP_EOL, 'UTC time: ', $db->GetOne('SELECT NOW()'); echo PHP_EOL, 'lc_time_names: ', $db->GetOne('SELECT @@lc_time_names'); echo ': ', $db->PGetOne("SELECT DATE_FORMAT(?,'%W %a %M %b')", '2010-01-01'); $db->PExecute('SET lc_time_names=?', 'es_MX'); echo PHP_EOL, 'lc_time_names: ', $db->GetOne('SELECT @@lc_time_names'); echo ': ', $db->PGetOne("SELECT DATE_FORMAT(?,'%W %a %M %b')", '2010-01-01'); $db->PExecute('SET lc_time_names=?', 'pt_BR'); echo PHP_EOL, 'lc_time_names: ', $db->GetOne('SELECT @@lc_time_names'); echo ': ', $db->PGetOne("SELECT DATE_FORMAT(?,'%W %a %M %b')", '2010-01-01'); # ------------------------------------------------------------------------------ echo PHP_EOL, str_repeat('-', 80), PHP_EOL, 'Time: ', $timer->getPageLoadTime(), ' - Memory: ', $timer->getMemoryUsage(1), PHP_EOL, str_repeat('-', 80), PHP_EOL;
<?php require_once '../../MPLT.php'; $timer = new MPLT(); require_once '../../src/dalmp.php'; # ------------------------------------------------------------------------------ $user = getenv('MYSQL_USER') ?: 'root'; $password = getenv('MYSQL_PASS') ?: ''; $host = getenv('MYSQL_HOST') ?: '127.0.0.1'; $port = getenv('MYSQL_PORT') ?: '3306'; $db = new DALMP\Database("utf8://{$user}:{$password}@{$host}:{$port}/dalmp"); /** * load zone files to mysql * mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root mysql */ $db->PExecute('SET time_zone=?', '+00:00'); $db->FetchMode('ASSOC'); $sql = 'SELECT Name, Continent FROM Country WHERE Population > ? AND Code LIKE ? LIMIT ?'; $rs = $db->PGetAll($sql, 10000000, '%P%', 2); print_r($rs); $rs = $db->Execute('DROP TABLE IF EXISTS `tests`'); $rs = $db->Execute('CREATE TABLE `tests` (id INT(11) unsigned NOT NULL AUTO_INCREMENT, col1 varchar(255), col2 varchar(255), col3 varchar(255), status iNT(1), PRIMARY KEY (id))'); $rs = $db->AutoExecute('tests', array('col1' => 'ai eu', 'col2' => 2, 'status' => 0)); /** * status value is 0 or 1 on table * NOTICE the use of === */ $sql = 'SELECT status FROM tests WHERE id=?'; $rs = $db->PgetOne($sql, 3); if ($rs === false) { echo "no result" . $timer->isCli(1);
/** * doing the same but consuming more memory. * Below the returned $rs2 array is not referential. Because of that, the system * will use excesive memory. With large columns. */ $rs2 = $db->GetAll($sql); foreach ($rs2 as $value) { list($r1, $r2, $r3) = $value; echo "f1: {$r1}, f2: {$r2}, f3: {$r3}", $timer->isCli(1); } $timer->setMark('foreach'); /** * prepared statements * and array needs to be passed as an argument */ $rs = $db->PExecute('SELECT * FROM Country WHERE Continent = ?', 'Europe'); $out = array(); while ($rows = $db->Pquery($out)) { print_r($out); } $rs = $db->PExecute('UPDATE Country SET code=? WHERE Code="PRT"', 'PRT'); /** * Returns the number of rows affected by INSERT, UPDATE, or DELETE query. * an UPDATE prepared statement which contains the same data as that already * in the database returns 0 for affected_rows */ echo $db->getNumOfRowsAffected(), PHP_EOL; $timer->setMark('stmt'); echo $timer->isCli(1); # ------------------------------------------------------------------------------ echo PHP_EOL, str_repeat('-', 80), PHP_EOL;