Skip to content

Instantly share code, notes, and snippets.

@MrSaints
Last active November 21, 2022 12:32
Show Gist options
  • Save MrSaints/8209879 to your computer and use it in GitHub Desktop.
Save MrSaints/8209879 to your computer and use it in GitHub Desktop.
A custom database class to demonstrate how WordPress' wpdb class can be instantiated and extended to allow you to connect to another / an external or secondary database. The global $wpdb object is only capable of communicating with the WordPress database. Refer to http://codex.wordpress.org/Class_Reference/wpdb for more information.
<?php
/*
The MIT License (MIT)
Copyright © 2014 Ian Lai
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.
*/
define('DB_HOST', '127.0.0.1');
define('DB_USER', 'root');
define('DB_PASS', 'password');
define('DB_NAME', 'school');
class CustomDatabase
{
private $db;
public function __construct() {
// Connect To Database
try {
$this->db = new wpdb(DB_USER, DB_PASS, DB_NAME, DB_HOST);
$this->db->show_errors(); // Debug
} catch (Exception $e) { // Database Error
echo $e->getMessage();
}
}
// Example Query -- Count Students
public function count_users() {
return $this->db->get_var("SELECT COUNT(*) FROM `students`;--");
}
// Example Query #2 -- Update Student Name
public function update_student_id($student_id, $student_name) {
return $this->db->update(
'students',
array('student_name' => $student_name),
array('student_id' => $student_id),
array('%s'), array('%d')
);
}
}
$Custom_DB = new CustomDatabase;
?>
@bhende19
Copy link

bhende19 commented Nov 1, 2015

I love this concept and it solve one of three issues for me - the unsolved details are:

  1. Where do you put this new class to prevent it from being overwritten when WP is updated and

  2. where to Instantiate it -

@MrSaints
Copy link
Author

MrSaints commented Aug 6, 2016

@bhende19 apologies for the tardy response, but in case anyone is curious... this was originally designed for use in something like a plugin. So to answer 1. and 2., it is exactly that. Place it in plugin folder to avoid being overwritten, and instantiate it there. You can check if the class is defined beforehand to avoid collision. I don't know how relevant this file still is since it's been over 2 years...

@muhammadasadnazeertest
Copy link

which location, i am add this file.

@rahuldev345
Copy link

I tried it but it doesn't seems to be catching errors when connection fails. Know any reason?

try {
	$mydb = new wpdb( $this->username, $this->password, $this->database, $this->hostname );
	$mydb->show_errors();
} catch( Exception $e ) {
	echo $e->getMessage();
}

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