Example #1
0
/**
 * Used to add data to database
 * 
 * @since 1.2.2
 * @param array $connection_information an array with following keys:
 * host => the database host name
 * user => the database user
 * password => the database password
 * database => the database name
 * debug => the debug level it can be 0,1 or 2
 * charset => utf8
 */
function InsertQuery($connection_information)
{
    /** The database connection details */
    $parameters = $connection_information;
    /** The DatabaseFunctions object is created */
    $database = new DatabaseFunctions($parameters);
    /** Auto commit is disabled. So the data will only be saved to database when the commit function is called */
    $database->df_toggle_autocommit(false);
    /** The database table name */
    $table_name = "pakphp_cached_data";
    /** The table name is set */
    $database->df_set_table($table_name);
    /** The fields to add */
    $main_query = array();
    /** Field 1 */
    $main_query[0]['field'] = "function_name";
    /** Value for field 1 */
    $main_query[0]['value'] = "InsertQuery";
    /** Field 2 */
    $main_query[1]['field'] = "function_parameters";
    /** Value for field 2 */
    $main_query[1]['value'] = "test parameters";
    /** Field 3 */
    $main_query[2]['field'] = "data";
    /** Value for field 3 */
    $main_query[2]['value'] = "test data";
    /** Field 4 */
    $main_query[3]['field'] = "created_on";
    /** Value for field 4 */
    $main_query[3]['value'] = time();
    /** The database query is fetched */
    $query = $database->df_build_query($main_query, array(), 'i');
    echo "<h3>Database query: </h3>";
    /** The query is displayed */
    echo $query;
    /** The database query is run */
    $database->df_execute($query);
    /** The number of rows affected by the query */
    $affected_rows = $database->df_affected_rows($query);
    echo "<h3>Affected rows: </h3>";
    print_r($affected_rows);
    /** The id of the last inserted row */
    $last_inserted_row_id = $database->df_last_insert_id();
    echo "<h3>Last inserted row id: </h3>";
    print_r($last_inserted_row_id);
    /**
     * The data is commited to database
     * If you comment out the following line, and uncomment the rollback line
     * Then the data will not be added to database
     * But in both cases the mysql affected rows shows 1 record added
     */
    $database->df_commit();
    /** The changes are rolled back */
    //$database->df_rollback();
    /** The query log is displayed */
    echo "<h3>Query Log: </h3>";
    $database->df_display_query_log(false);
    /** The query log is cleared */
    $database->df_clear_query_log();
    /** The database connection is closed */
    $database->df_close();
}