MySQLi Functions

MySQLi Functions

The MySQLi functions will allow your code to interact with your site's MySQLi database.

MySQLi Class File:

The MySQLi Class File is located in the inc directory. The MySQLi class functions are located in the class.mysqli.php file.


Function: Connect

Connects to the MySQLi Database

Parameters:

  • (string) MySQLi Host Name
  • (string) MySQLi username
  • (string) MySQLi password
  • (string) MySQLi database name
  • (bool) (optional) If true, creates a persistant MySQLi connection

Return Value:

Returns true on success, false on connection error.

Example Usage:

$status = $this->db->Connect("localhost", "root", "", "rusnakcms-demo");

Function: Query

Executes a MySQLi Query on the database

Parameters:

  • (string) MySQL Query - Query to run on database

Return Value:

Returns true on success, false on error.

In the event of an error, you can call $this->db->sLastError to get the error.

Example Usage:

// Run the Query $queryResult = $this->db->Query("SELECT * FROM ".$this->prefix."modules"); // Get the number of records returned by query $queryNum = $this->db->iRecords; if($queryNum > 0){ // Fetch results as assoc array $row = $this->db->getResult(); // Save the query results $module_name = $row['content_name']; $module_folder = $row['module_folder']; $module_file = $row['module_file']; }

Function: secure

Secures a piece of data for entry into the database. Runs mysqli_real_escape_string on the data to protect against SQL injection and runs strip_tags and htmlentities on the data to prevent against unwanted HTML / Cross Site Scripting attacks.

Parameters:

  • (string) Data to be secured

Return Value:

Data secured with mysqli_real_escape_string and with HTML removed

Example Usage:
// Disable an enabled page $pageToDisable = $this->db->secure($_GET["pageID"]);

Function: escape

Secures a piece of data for entry into the database by calling mysqli_real_escape_string. Unlike secure, escape keeps HTML in the data intact.

Parameters:

  • (string OR array) Data to be secured

Return Value:

Returns a properly escaped string or array, depending on data passed to it

Example Usage:
// Disable an enabled page $pageToDisable = $this->db->escape($_GET["pageID"]);

Function: getResult

Fetches query results as an associative array.

Parameters:

  • NONE

Return Value:

Returns an associative array of the current database row, or FALSE if no more rows. Essentially, an alias for mysqli_fetch_assoc.

Example Usage:
$this->db->Query("SELECT * FROM ".$this->prefix."pages"); $queryNum = $this->db->iRecords; if($queryNum > 0){ // Loop through each row of the result set while ($row = $this->db->getResult()){ $pageID = $row['id']; $pageTitle = $row['pageTitle']; $pageURL = $row['pageURL']; $pageSpecial = $row['pageSpecial']; $pageInNav = $row['pageInNav']; $pageEnabledStatus = $row['pageEnabled']; // Do something with the collected data } }

Function: getResultSpecific

Fetches query results as an associative array.

Parameters:

  • (object) MySQLi Result Set Identifier

Return Value:

Returns an associative array of the current database row, or FALSE if no more rows. Essentially, an alias for mysqli_fetch_assoc.


Function: getLastInsertID

Gets the ID of the last row inserted in the database using an INSERT query

Parameters:

  • NONE

Return Value:

Returns an integer value of the last inserted ID


Function: prepareQuery

Prepares a MySQL query to be used with MySQLi prepared statements

Parameters:

  • (string) The MySQL query to prepare

Return Value:

Returns a statement object that can be used with MySQLi prepared statements

Example Usage:

$stmt = $this->db->prepareQuery("SELECT title, post, datePosted, tags FROM ".$this->prefix."blogposts WHERE slug = ? AND enabled = 1 AND datePosted <= NOW() LIMIT 1"); $stmt->bind_param("s", $page_slug); $res = $stmt->execute(); $stmt->bind_result($post_title, $post_content, $date_posted, $post_tags); if($res){ $row = $stmt->fetch(); // Do something with $post_title, $post_content, etc }

Variable: sLastError

Holds the last error returned by MySQL

Example Usage:

$dbError = $this->db->sLastError;

Variable: sLastQuery

Holds the last non-prepared query run on the database


Variable: sLastPrepQuery

Holds the last prepared query run on the database


Variable: iRecords

Holds the number of records returned by MySQL


Variable: iAffected

Holds the number of rows affected by the last MySQL query


Variable: $this->prefix

Contains the database prefix used on your site. The default prefix is "rusnak_".


Object: sDBLink

The MySQL database connection link