Beispiel #1
0
function delete_query(Db_Mysql $db)
{
    $id = 15;
    $stmt = "DELETE FROM user\n             WHERE user_id = '{$id}'";
    // 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;
    }
}
Beispiel #2
0
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;
    }
}
Beispiel #3
0
function select_query(Db_Mysql $db)
{
    $stmt = "SELECT * FROM user";
    // 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();
        // if there are some rows
        if ($num_rows > 0) {
            $rows = array();
            // fetch a result row as an associative array
            while ($row = $db->fetch($result, MYSQLI_ASSOC)) {
                $rows[] = array('fullname' => $row['fullname'], 'email' => $row['email'], 'country' => $row['country']);
            }
            // prints results
            $content = '<div class="table-responsive"><table class="table table-condensed">
                            <thead>
                                <tr>
                                    <th>Full name</th>
                                    <th>Email</th>
                                    <th>Country</th>
                                </tr>
                            </thead>';
            foreach ($rows as $iter) {
                $content .= '<tbody>
                            <tr>
                                <td>' . $iter['fullname'] . '</td>
                                <td><a href="mailto:' . $iter['email'] . '">' . $iter['email'] . '</a></td>
                                <td>' . $iter['country'] . '</td>
                            </tr>';
            }
            $content .= '</tbody></table></div>';
            echo $content;
            echo 'Number of affected rows: ' . $num_rows . '<br>';
            echo 'Auto generated id used in the last query: ' . $insert_id;
        } else {
            echo 'There are no rows in the database.';
        }
    }
}
Beispiel #4
0
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;
    }
}
<div style="padding: 20% 20%;">
	<a href="index.php">Index</a>
	<a href="insert.php">Insert</a>
	<a href="select.php">Select</a>
	<a href="update.php">Update</a>
	<a href="delete.php">Delete</a>
</div>
<?php 
require_once 'db/mysql.php';
$db = new Db_Mysql(array('dbname' => 'shift_planning_test', 'username' => 'root', 'password' => '', 'host' => 'localhost'));
$fullname = "Konstantin Petrovic";
$email = "*****@*****.**";
$country = "Serbia";
$query_update = "UPDATE user SET fullname = '{$fullname}', email = '{$email}', country = '{$country}' WHERE user_id=20";
$db->connect();
$result = $db->query($query_update);
if ($result == false) {
    echo $db->error() . " " . $db->errno();
} else {
    $update = $db->affectedRows();
    echo $update;
}
$db->close();