function insert_query(Db_Mysql $db) { $fullname = $db->escape('Miloš Savić'); $email = $db->escape('*****@*****.**'); $country = $db->escape('Serbia'); $stmt = "INSERT INTO user (fullname, email, country)\n VALUES ('{$fullname}', '{$email}', '{$country}')"; // query result $result = $db->query($stmt); if (!$result) { echo $db->errno() . ' ' . $db->error(); } else { // gets the number of affected rows $num_rows = $db->affectedRows(); // gets the auto generated id used in the last query $insert_id = $db->insertId(); echo 'Number of affected rows: ' . $num_rows . '<br>'; echo 'Auto generated id used in the last query: ' . $insert_id; } }
function update_query(Db_Mysql $db) { $country = $db->escape('Serbia'); $stmt = "UPDATE user\n SET country = '{$country}'\n WHERE country = 'Burundi'"; // query result $result = $db->query($stmt); if (!$result) { echo $db->errno() . ' ' . $db->error(); } else { // gets the number of affected rows $num_rows = $db->affectedRows(); // gets the auto generated id used in the last query $insert_id = $db->insertId(); echo 'Number of affected rows: ' . $num_rows . '<br>'; echo 'Auto generated id used in the last query: ' . $insert_id; } }
<?php require_once 'db/mysql.php'; $db = new Db_Mysql(array('dbname' => 'shift_planning_test', 'username' => 'root', 'password' => 'root', 'host' => 'localhost')); $id = "20"; $query = "DELETE FROM user WHERE user_id=" . $id; $db->connect(); $db->escape($query); $db->query($query); $db->close(); header('Location: index.php');
<?php ini_set('display_errors', 'on'); error_reporting(E_ALL); require_once 'db/mysql.php'; require 'select.php'; require 'delete.php'; require 'insert.php'; require 'update.php'; $db = new Db_Mysql(array('dbname' => 'shift_planning_test', 'username' => 'root', 'password' => NULL, 'host' => 'localhost')); $db->connect(); echo '<br>'; $db->ping(); echo '<br>'; $result = $db->query(SELECT::$query); $db->fetch($result, $resultType = null); echo '<br>'; $db->affectedRows(); echo '<br>'; $db->insertId(); echo '<br>'; $unescapedString = "ASDASDAS"; $db->escape($unescapedString); echo '<br>'; $db->close();