Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ####################### getUsers.php file ############################
- <?php
- /** MySQL settings **/
- /** The name of the database for WordPress **/
- define('DB_NAME', 'test_db');
- /** MySQL database username */
- define('DB_USER', 'testuser');
- /** MySQL database password */
- define('DB_PASSWORD', 'testpass');
- /** MySQL hostname */
- define('DB_HOST', 'localhost');
- require_once('application/classes/database.php');
- function die_r($value)
- {
- echo '<pre>';
- print_r($value);
- echo '</pre>';
- die();
- }
- $db = new Database();
- $getRow = $db->getRow("SELECT * FROM users WHERE userID = ?", ["1"]);
- die_r($getRow);
- ###################### database.php file ###############################
- <?php
- /****** SETUP DATABASE CLASS ******/
- class Database {
- /*** CONNECT TO THE DATABASE ***/
- public $isConnected;
- protected $datab;
- public function __construct($dbhost = DB_HOST, $dbname = DB_NAME, $dbuser = DB_USER, $dbpass = DB_PASSWORD)
- {
- $this->isConn = TRUE;
- try{
- $this->datab = new PDO ( "mysql:host={$dbhost};dbname={$dbname};charset=utf8", $dbuser, $dbpass);
- $this->datab->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
- $this->datab->setAttribute( PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC );
- } catch ( PDOException $e ) {
- $this->isConn = FALSE;
- throw new Exception ($e->getMessage () );
- }
- }
- /*** DISCONNECT FROM DATABASE ***/
- public function Disconnect(){
- $this->datab = NULL;
- $this->isConn = FALSE;
- }
- /*** GET ROW ***/
- public function getRow($query, $params = []){
- try {
- $stmt = $this->datab->prepare($query);
- $stmt->execute($params);
- return $stmt->fetch();
- } catch ( PDOException $e ) {
- throw new Exception ($e->getMessage () );
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement