PHP Classes

File: Example

Recommend this page to a friend!
  Classes of Eric Lakich   Mysqli Query Parameterization Class   Example   Download  
File: Example
Role: Example script
Content type: text/plain
Description: Example file demonstrating class use
Class: Mysqli Query Parameterization Class
Execute prepared queries with MySQLi extension
Author: By
Last change: Minor update to remove the closing php tag
Date: 12 years ago
Size: 1,412 bytes
 

Contents

Class file image Download
<?php
# Example file for mysqli parameterized query class
include('class.mysqli.php');

# Initialize class object
$db = new mysqli_db;

# Set Database connection properties
$db->database_host = 'hostname';
$db->database_user = 'username';
$db->database_pass = 'password';
$db->database_name = 'database';

# Initialize the db connection
$db->open_mysqli_db();

# Set the database query
$db->set_query('Select * from table WHERE row_id > ? and column_name = ?');

# Push arguments
# NOTE: the first value is the value to push...the second value is the data type
# Valid data types are i, d, s, b (integer, double, string, blob)
# the datatypes are limited to the set allowed by the mysqli class
# The number of args MUST match the number of parameters '?' in the query
$db->push_argument(0,'i');
$db->push_argument('some value','s');

# Get the result set
$result = $db->get_results();

# Close the db conneciton
$db->close_mysqli_db();
   

# If the query used a select statement you now have an array of results.
# if the query used any other statement you have the id created/modified

# In this example we used a select statement so we expect and array of column values
#NOTE: we use the column names as the column array indexes

foreach ($result as $row=>$column) {
    echo
$column['row_id'];
    echo
$column['column_name'];
    echo
$column['another_column_name'];
}