Banshee
the secure PHP framework

Database library

Properties

  • boolean connected (r): Connection to database successful.
  • integer last_insert_id (r): Id of last inserted row.
  • integer affected_rows (r): Number of affected rows of last query.
  • string error (r): Last error message.
  • integer errno (r): Last error code.

Methods

  • integer last_insert_id(integer $history = 0):
    When history is set to 0, it returns the insert id of the last query. When history is set to 1, it returns the insert id of the query before the last query. And so on.
  • resource query(string $format[, mixed parameters, ...]):
    Performs a database query. This function should be used like the printf function.
  • array fetch(resource $resource):
    Returns the next item from the result set of the query resource, returned by query().
  • boolean free_result($resource):
    Free the query resource.
  • mixed execute(string $format[, mixed parameters, ...]):
    Performs a database query like query() and returns the entire result set.
  • boolean transaction(array $queries):
    Performs a database transation. The $queries array holds the queries, where each element is an array containing the query format and its parameters.
  • entry(string $table, string $id, string $col = "id"):
    Returns the entry of a table where id is equal to $id.
  • mixed insert(string $table, array $data, array $keys = null):
    Insert a new row in the database. $data holds the values for the new row and $keys the column names. If $keys is equal to null, the keys of the array $data will be used.
  • mixed update(string $table, integer $id, array $data, array $keys = null):
    Updates the row in the database where id is equal to $id.
  • mixed delete(string $table, integer $id):
    Deletes the row in the table where id is equal to $id.

Example

[php] connected == false) { exit; } if (($result = $db->execute("select * from table where id=%d", 41)) !== false) { foreach ($result as $item) { /* do stuff */ } } if (($resource = $db->query("select * from table where name=%s", $name)) !== false) { while (($row = $db->fetch($resource)) !== false) { /* do stuff */ } } $data = array( "int" => 123, "str" => "abc"); $result = $db->insert("table", $data); $result = $db->update("table", 84, $data); $result = $db->delete("table", 29); $keys = array("id", "int", "str"); $user_input = $_POST; $user_input["id"] = null; $result = $db->insert("table", $user_input, $keys); ?> [/php]