Skip to content

Instantly share code, notes, and snippets.

@anjerodesu
Last active September 23, 2022 05:13
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save anjerodesu/4502474 to your computer and use it in GitHub Desktop.
Save anjerodesu/4502474 to your computer and use it in GitHub Desktop.
DBClass is a simple PHP database class using MySQL Improved Extension.
<?php
/********************************************************************************************/
/* DBClass created by Angelo Villegas [http://www.studiovillegas.com] */
/* */
/* Copyright © Angelo Villegas */
/* Permission is hereby granted, free of charge, to any person obtaining a copy of this */
/* software and associated documentation files (the "Software"), to deal in the Software */
/* without restriction, including without limitation the rights to use, copy, modify, */
/* merge, publish, distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to the following */
/* conditions: */
/* */
/* The above copyright notice and this permission notice shall be included in all copies */
/* or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, */
/* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A */
/* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT */
/* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF */
/* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE */
/* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/********************************************************************************************/
require_once( 'DBSettings.php' );
//Database class to connect to database and fire queries
class DBClass extends DatabaseSettings
{
var $classQuery;
var $link;
var $errno = '';
var $error = '';
// Connects to the database
function DBClass()
{
// Load settings from parent class
$settings = DatabaseSettings::getSettings();
// Get the main settings from the array we just loaded
$host = $settings['dbhost'];
$name = $settings['dbname'];
$user = $settings['dbusername'];
$pass = $settings['dbpassword'];
// Connect to the database
$this->link = new mysqli( $host , $user , $pass , $name );
}
// Executes a database query
function query( $query )
{
$this->classQuery = $query;
return $this->link->query( $query );
}
function escapeString( $query )
{
return $this->link->escape_string( $query );
}
// Get the data return int result
function numRows( $result )
{
return $result->num_rows;
}
function lastInsertedID()
{
return $this->link->insert_id;
}
// Get query using assoc method
function fetchAssoc( $result )
{
return $result->fetch_assoc();
}
// Gets array of query results
function fetchArray( $result , $resultType = MYSQLI_ASSOC )
{
return $result->fetch_array( $resultType );
}
// Fetches all result rows as an associative array, a numeric array, or both
function fetchAll( $result , $resultType = MYSQLI_ASSOC )
{
return $result->fetch_all( $resultType );
}
// Get a result row as an enumerated array
function fetchRow( $result )
{
return $result->fetch_row();
}
// Free all MySQL result memory
function freeResult( $result )
{
$this->link->free_result( $result );
}
//Closes the database connection
function close()
{
$this->link->close();
}
function sql_error()
{
if( empty( $error ) )
{
$errno = $this->link->errno;
$error = $this->link->error;
}
return $errno . ' : ' . $error;
}
}
?>
<?php
/********************************************************************************************/
/* DBClass created by Angelo Villegas [http://www.studiovillegas.com] */
/* DBSettings.php */
/* */
/* Copyright © Angelo Villegas */
/* Permission is hereby granted, free of charge, to any person obtaining a copy of this */
/* software and associated documentation files (the "Software"), to deal in the Software */
/* without restriction, including without limitation the rights to use, copy, modify, */
/* merge, publish, distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to the following */
/* conditions: */
/* */
/* The above copyright notice and this permission notice shall be included in all copies */
/* or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, */
/* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A */
/* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT */
/* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF */
/* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE */
/* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/********************************************************************************************/
class DatabaseSettings
{
var $settings;
function getSettings()
{
// Database variables
// Host name
$settings['dbhost'] = '';
// Database name
$settings['dbname'] = '';
// Username
$settings['dbusername'] = '';
// Password
$settings['dbpassword'] = '';
return $settings;
}
}
?>
@LukeMcLachlan
Copy link

LukeMcLachlan commented Jul 29, 2018

delete, insert, update?

@onumanfj
Copy link

`$query="INSERT INTO users (email,password,phone,city) VALUES ('$_POST[email]','$pwgen','$_POST[phone]','$_POST[city]')";

$link->query($query);`

I'm trying to insert email, password, phone number, city and auto generated password to the table. It's not working. I need help

@biniyam17
Copy link

This is awesome! Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment