/** * get id by email * @param string $email * @return null|int id */ public function getIdByEmail($email) { //sql check $email = $this->connection->getEscapedString($email); //get id and return return $this->operations->getValue("id", "email = '{$email}'"); }
/** * Stores a given notification in the provided db * * @param ClientNotification $notification A notification to be store in the specified db * @return int the id of notification inserted * @throws DatabaseException If an error occured in the storing of the notification in the db */ public function store(ClientNotification $notification) { //String conversion and escaping in order to perform a correct and safe sql query $user_id = $notification->getUserId(); $message = $this->dbConnection->getEscapedString((string) $notification->getMessage()); $mediums = $notification->getNotificationMediums(); try { //Notification insert $this->dbOperations->insert('user_id,message', $user_id . ",'" . $message . "'"); //Id of the record just inserted $recordId = $this->dbOperations->getLastId(); if (is_array($mediums)) { foreach ($mediums as $medium) { //TODO this code doesn't work, use the code in the 'else' branch. Since the code will become the same it's a better choice to insert it into a method $this->dbOperations->insert('notification_id,type_id', $recordId . ", '" . get_class($medium) . "'", $this->notificationTypeTable); } } else { $className = get_class($mediums); $queryResult = $this->dbOperations->getValue('id', 'name = ' . "'" . $className . "'", $this->typesTable); if (is_null($queryResult)) { $this->dbOperations->insert('name', "'" . $className . "'", $this->typesTable); $queryResult = $this->dbOperations->getLastId(); } $this->dbOperations->insert('notification_id,type_id', $recordId . ", " . $queryResult, $this->notificationTypeTable); } return $recordId; } catch (MysqltcsException $e) { throw new DatabaseException('An error occurred in the insertion of the data in the database', 0, $e); } }
require_once __DIR__ . "/../vendor/autoload.php"; $db = (require __DIR__ . "/config.php"); print "<h1>Connection</h1>\n"; //catch wrong connection print "<h2>Test connection</h2>\n"; print "<h3>Wrong data</h3>\n"; try { $connection = new Mysqltcs($db['host'], $db['user'] . "wrong", $db['psw'], $db['db']); } catch (MysqltcsException $e) { print "Error on connection caught<br/>\n"; print $e->getMessage() . "<br/>\n"; } print "<h3>Correct data</h3>\n"; //correct connection try { $connection = new Mysqltcs($db['host'], $db['user'], $db['psw'], $db['db']); print "No error<br/>\n"; } catch (MysqltcsException $e) { } print "<h2>Simple query</h2>\n"; print "<h3>Correct query(show tables)</h3>\n"; //simple query $results = $connection->executeQuery("show tables"); while ($row = $results->fetch_array()) { print $row[0] . "<br/>\n"; } print "<h3>Error on query</h3>\n"; //if an error is occurred mysqltcs throw an exception try { $results = $connection->executeQuery("No sql"); } catch (MysqltcsException $e) {
<?php /** * Created by PhpStorm. * User: claudio * Date: 18/07/15 * Time: 16.00 * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ use it\thecsea\mysqltcs\Mysqltcs; require_once __DIR__ . "/../vendor/autoload.php"; $db = (require __DIR__ . "/config.php"); print "create two connections with same parameters (you have to set to false the parameter number 5)<br>\n"; $connection = new Mysqltcs($db['host'], $db['user'], $db['psw'], $db['db'], false); $connection2 = new Mysqltcs($db['host'], $db['user'], $db['psw'], $db['db'], false); print "they represent the same connection(thread id)<br/>\n"; print $connection->getConnectionThreadId() . "<br/>\n"; print $connection2->getConnectionThreadId() . "<br/>\n";
* as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ use it\thecsea\mysqltcs\Mysqltcs; use it\thecsea\mysqltcs\SimpleLogger; use it\thecsea\mysqltcs\MysqltcsException; require_once __DIR__ . "/../vendor/autoload.php"; $db = (require __DIR__ . "/config.php"); $connection = new Mysqltcs($db['host'], $db['user'], $db['psw'], $db['db'], false); print "create logger<br>\n"; //you can create your logger that implements MysqltcsLogger $logger = new SimpleLogger(); $connection->setLogger($logger); //you can als do in the following way: $connection->setSimpleLogger() and get logger with $connection->getLogger(); print "Simple correct query<br/>\n"; $connection->executeQuery("show tables"); print "Query with error<br/>\n"; try { $connection->executeQuery("no sql"); } catch (MysqltcsException $e) { } print "Log data<br/>\n"; $data = $logger->getLogArray(); foreach ($data as $ele) {