Terms - warwickfoster/qurantools GitHub Wiki

File: app/library/database.php

Purpose

This file contains a collection of commonly used functions for database operations, such as connecting to the database, querying the database, and escaping values.

Functions

  • db_connect() Establishes a connection to the database.
  • db_error() Logs or reports database errors.
  • db_query() Executes a database query.
  • db_quote() Escapes a value for safe inclusion in a MySQL statement.
  • db_quote_array() Applies db_quote to each element in an array.
  • db_rowcount() Returns the number of rows in a result set.
  • db_return_row() Returns the next database row as an associative array.
  • db_goto() Moves the result set pointer to a specific record.
  • debug_to_console() Outputs debugging information to the browser console.
  • db_return_one_record_one_field() Returns the value of the first field in the first row of a result set.

Usage

These functions can be used in other PHP files to perform database operations. For example:

// Connect to the database
$db_connection = db_connect();

// Query the database
$result = db_query("SELECT * FROM users");

// Print the number of rows
echo "Number of users: " . db_rowcount($result);

Database Connection

The db_connect() function handles database connection and error handling. It uses a static variable to ensure that the connection is maintained between function calls.

Query Execution

The db_query() function executes a database query and returns a result set. It checks for errors and logs them if necessary.

Value Escaping

The db_quote() function escapes values to prevent SQL injection attacks.

Error Handling

The db_error() function logs errors to the browser console or server logs, depending on the configuration.

Additional Features

  • db_goto() allows you to navigate the result set.
  • debug_to_console() provides a convenient way to debug PHP code.
  • db_return_one_record_one_field() is useful for retrieving single values from the database.

Conclusion

The library/common.php file provides a comprehensive set of functions for database operations, making it easier to work with databases in PHP applications.