PHP Classes

MySQLi Complete Class: Perform SQL queries from parameters using MySQLi

Recommend this page to a friend!
  Info   View files Example   View files View files (3)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
StarStarStarStar 70%Total: 548 This week: 1All time: 5,523 This week: 560Up
Version License PHP version Categories
mysqli-complete 1.0.0Freely Distributable5PHP 5, Databases, Tools
Description 

Author

This package can perform SQL queries from parameters using MySQLi.

It can connect to a MySQL database using MySQLi and executes common SQL queries composed using given parameters like table names, fields, field values and conditions. Currently it can execute:

- SELECT queries of given table fields and condition
- INSERT queries with given table field values
- UPDATE queries with given table field values and condition
- DELETE queries on given table and condition
- List fields of a given table

Picture of Marcelo Franco
  Performance   Level  
Name: Marcelo Franco <contact>
Classes: 1 package by
Country: Brazil Brazil
Age: 61
All time rank: 3171233 in Brazil Brazil
Week rank: 411 Up35 in Brazil Brazil Up

Example

<?php

// CONFIG
define("DBHOST", "YOUR HOST");
define("DBUSER", "YOUR USER");
define("DBPASS", "YOUR PASSWORD");
define("DBNAME", "YOUR DATABASE NAME");

require_once(
"mysqli.php");

$db = new dbConn();

   
// GENERIC QUERY EXECUTE
   
echo "GENERIC QUERY EXECUTE:<br />";
   
$a = $db->query("select * from _Users");
    foreach (
$a as $b) {
        echo
"ID [" . $b["UserID"] . "] - Name [" . $b["UserName"] . "] - Mail [" . $b["UserMail"] . "]<br />";
    }
   
$a->close();

   
   
// SELECT
   
echo "<br />SELECT:<br />";
    if (
$r = $db->select("UserID, UserName, UserMail", "_Users", "where UserID=1")) {
        echo
"ID [" . $r["UserID"] . "] - Name [" . $r["UserName"] . "] - Mail [" . $r["UserMail"] . "]<br />";
    } else {
        echo
"- No Record Found!<br />";
    }
    unset(
$r);

   
   
// SELECT GROUP
   
echo "<br />SELECT GROUP:<br />";
   
$d = $db->selectGroup("*", "_Users", "LIMIT 10");
    if (
$d->num_rows > 0) {
        while(
$r = $d->fetch_assoc() ) {
            echo
"ID [" . $r["UserID"] . "] - Name [" . $r["UserName"] . "] - Mail [" . $r["UserMail"] . "]<br />";
        }
    } else {
        echo
"- No Records Found!<br />";
    }
    echo
"- Number of Records [" . $d->num_rows . "]<br />";
   
$d->close();
   
   
   
// INSERT
   
echo "<br />INSERT:<br />";
   
$t = array();
   
$t["UserName"] = "Industria Virtual 1";
   
$t["UserMail"] = "email1@industriavirtual.com.br";
    if (
$db->insert("_Users", $t)) {
       
$i = $db->insert_id();
        echo
"- New Record ID [" . $i . "]<br />";
    } else {
        echo
"- Error Inserting Data!<br />";
    }

   
   
// UPDATE
   
echo "<br />UPDATE:<br />";
   
$t = array();
   
$t["UserName"] = "Industria Virtual 2";
   
$t["UserMail"] = "email2@industriavirtual.com.br";
    if (
$db->update("_Users", $t, "WHERE UserID=" . $i)) {
        echo
"- Record " . $i . " Renamed!<br />";
    } else {
        echo
"- Error Renamming!<br />";
    }
   
   
   
// DELETE
   
echo "<br />DELETE:<br />";
    if (
$db->delete("_Users", "WHERE UserID=" . $i)) {
        echo
"- Record " . $i . " Deleted!<br />";
    } else {
        echo
"- Delete Error!<br />";
    }
   

   
// LIST TABLES ON DATABASE:
   
echo "<br />TABLES LIST:<br />";
   
$tables = $db->listTables();
   
$arrlength = count($tables);
    for(
$x = 0; $x < $arrlength; $x++) {
        echo
$tables[$x] . "<br />";
    }
   
   
   
// LIST FIELDS ON TABLE:
   
echo "<br />FIELDS LIST:<br />";
   
$fields = $db->listFields("_Users");
   
$arrlength = count($fields);
    for(
$x = 0; $x < $arrlength; $x++) {
        echo
"Field [" . $fields[$x]['name'] . "] - Type [" . $fields[$x]['type'] . " (" . $fields[$x]['code'] . ")] - Max Length [" . $fields[$x]['size'] . "]<br />";
    }
   
   
$db->close();


Details

MySQLi CLASS ------------------------------------------------------------------------------------------------------------------------ Example Table: CREATE TABLE `_Users` ( `UserID` INT(11) NOT NULL AUTO_INCREMENT, `UserName` VARCHAR(100) NULL DEFAULT NULL, `UserMail` VARCHAR(180) NULL DEFAULT NULL, PRIMARY KEY (`UserID`) ) ENGINE=innoDB ; INSERT INTO `_Users` (`UserName`, `UserMail`) VALUES ('User 01 Name', 'user1@industriavirtual.com.br'); INSERT INTO `_Users` (`UserName`, `UserMail`) VALUES ('User 02 Name', 'user2@industriavirtual.com.br'); INSERT INTO `_Users` (`UserName`, `UserMail`) VALUES ('User 03 Name', 'user3@industriavirtual.com.br'); ------------------------------------------------------------------------------------------------------------------------ HOW TO USE (in your PHP code): 1) Define Database Connection: define("DBHOST", "host"); define("DBUSER", "user"); define("DBPASS", "password"); define("DBNAME", "database"); ------------------------------------------------------------------------------------------------------------------------ 2) Include Class File: require_once("mysqli.php"); ------------------------------------------------------------------------------------------------------------------------ 3) Open Connection: $db = new dbConn(); ------------------------------------------------------------------------------------------------------------------------ 3) Record Select Example: if ($r = $db->select("UserID, UserName, UserMail", "_Users", "where UserID=1")) { echo $r["UserMail"] . "<br />"; } else { echo "- No Record Found!<br />"; } unset($r); ------------------------------------------------------------------------------------------------------------------------ 3) Close Connection: $db->close(); ------------------------------------------------------------------------------------------------------------------------ NOTE: Optional Connection With Other Database: $db = new dbConn("host", "user", "password", "database"); ------------------------------------------------------------------------------------------------------------------------ OTHER INCLUDED FUNCTIONS: A) Select Group of Records Example: $d = $db->selectGroup("*", "_Users", "LIMIT 10"); while($r = $d->fetch_assoc() ) { echo $r["UserMail"] . "<br />"; } $d->close(); ------------------------------------------------------------------------------------------------------------------------ B) Insert Example: $t = array(); $t["UserName"] = "Industria Virtual 1"; $t["UserMail"] = "email1@industriavirtual.com.br"; $db->insert("_Users", $t) ------------------------------------------------------------------------------------------------------------------------ C) Update Example: $t = array(); $t["UserName"] = "Industria Virtual 2"; $t["UserMail"] = "email2@industriavirtual.com.br"; $db->update("_Users", $t, "WHERE UserID=1") ------------------------------------------------------------------------------------------------------------------------ D) Delete Example: $db->delete("_Users", "WHERE UserID=1") ------------------------------------------------------------------------------------------------------------------------ E) Free Query Execute: $a = $db->query("select * from _Users"); foreach ($a as $b) { echo $b["UserMail"] . "<br />"; } $a->close(); ------------------------------------------------------------------------------------------------------------------------ BONUS: I) List Tables in Database: $tables = $db->listTables(); $arrlength = count($tables); for($x = 0; $x < $arrlength; $x++) { echo $tables[$x] . "<br />"; } ------------------------------------------------------------------------------------------------------------------------ II) List Fields from Table: $fields = $db->listFields("_Users"); $arrlength = count($fields); for($x = 0; $x < $arrlength; $x++) { echo "Field [" . $fields[$x]['name'] . "] - Type [" . $fields[$x]['type'] . " (" . $fields[$x]['code'] . ")] - Max Length [" . $fields[$x]['size'] . "]<br />"; } ------------------------------------------------------------------------------------------------------------------------ Check file "example.php" for more information. Contact: Marcelo Franco (codes@industriavirtual.com.br)

  Files folder image Files  
File Role Description
Accessible without login Plain text file example.php Example Example of use.
Plain text file mysqli.php Class A complete class for MySQL database handling with standard MSQLI connection.
Accessible without login Plain text file readme.txt Doc. README file.

 Version Control Unique User Downloads Download Rankings  
 0%
Total:548
This week:1
All time:5,523
This week:560Up
 User Ratings  
 
 All time
Utility:90%StarStarStarStarStar
Consistency:90%StarStarStarStarStar
Documentation:80%StarStarStarStarStar
Examples:80%StarStarStarStarStar
Tests:-
Videos:-
Overall:70%StarStarStarStar
Rank:279