Is it feasible to code RevCMS style to be PDO?

FirefighterKyle

I am Kyle!!
Sep 14, 2012
162
7
So I have been out of the I guess basic coding
(Because I was never that good)
world for a while and I never moved on or advanced from PHP 4.3

After going through many pages on php.net for PDO it looks pretty simple to learn or well get into but I always want to go back to the way I knew or learned which was coding with RevCMS. I don't want to make anything for habbo but I am curious, is it possible to use a skeleton style of RevCMS?

Having a (Folder) that has global.php, index.php with folders for (app, interfaces, tpl, management, etc)
I like the clean look of it and the ease to edit stuff like class.engine.php, class.users.php, etc.

I am trying to make a skeleton design of it right now but am running into questions on how to do something like could you, even use a config.php or do you need multiple files to gather the database information?
 

FirefighterKyle

I am Kyle!!
Sep 14, 2012
162
7
Yes, I know that PDO is MySQL just new and well not dead lol. The problem where I run into is from the old stuff which I am looking at for a template to add
PHP:
class core implement iCore
{
    #codes
   
    #code
   
    #more-codes
}
Like the config.php for example everything I look at to set up the pdo database stuff is like:
PHP:
$hostname = "localhost";
$username = "username";
$password = "password";

try {
  $conn = new PDO("mysql:host=$hostname;dbname=myDB", $username, $password);
  // set the PDO error mode to exception
  $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  echo "Connected successfully";
} catch(PDOException $e) {
  echo "Connection failed: " . $e->getMessage();
}


/*
Vs RevCMS config
*/
if(!defined('IN_INDEX')) { die('Sorry, you cannot access this file.'); }

/*
*

* Database Management

*
*/

$_CONFIG['
mysql']['connection_type'] = 'connect';

$_CONFIG['mysql']['hostname'] = 'localhost'; // MySQL host | Example: localhost or 127.0.0.1

$_CONFIG['mysql']['username'] = 'root'; // MySQL username / loging name | Example: root

$_CONFIG['mysql']['password'] = 'Password'; // MySQL Password / leave blank if no PHPMyAdmin password | Example: 'Tester' or (if no password) ''

$_CONFIG['mysql']['database'] = 'database1'; // MySQL database / schema

$_CONFIG['mysql']['port'] = '3306'; // MySQL port

So my question is how would you implement pdo connecting to db vs the config part or am I looking at it wrong and it would be how the class.engine.php grabs it?
 

FirefighterKyle

I am Kyle!!
Sep 14, 2012
162
7
The config.php defines connection parameters.

Engine class initializes the connection.
Okay so config is pretty much this then

Code:
$hostname = "localhost";
$username = "username";
$password = "password";

where class.engine would be for the
Code:
$conn = new PDO("mysql:host=$hostname;dbname=myDB", $username, $password);
  // set the PDO error mode to exception
  $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  echo "Connected successfully";
} catch(PDOException $e) {
  echo "Connection failed: " . $e->getMessage();
Correct?

Also should I change it from the code like $_CONFIG['mysql']['code']; to $code?
 

JayC

Always Learning
Aug 8, 2013
5,493
1,398
Okay so config is pretty much this then

Code:
$hostname = "localhost";
$username = "username";
$password = "password";

where class.engine would be for the
Code:
$conn = new PDO("mysql:host=$hostname;dbname=myDB", $username, $password);
  // set the PDO error mode to exception
  $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  echo "Connected successfully";
} catch(PDOException $e) {
  echo "Connection failed: " . $e->getMessage();
Correct?
You would create a local copy of the connection and have helper functions for executing queries.
 

FirefighterKyle

I am Kyle!!
Sep 14, 2012
162
7
Okay, so I am trying a different approach and hoping that it will work, I mean why we try things to learn and have it break lol. So this is my engine so far not sure if it will work; if it's useless coding; or whatnot.
PHP:
<?php

namespace Rev;

use PDOStatement;

if(!defined('IN_INDEX')) { die('Sorry, you cannot access this file.'); }

    class engine
    {
        private $initiated;
        private $connected;

        private $connection;

        final public function Initiate()
        {
            global $_CONFIG;
           
            if(!$this->initiated)
            {
                $this->setPDO('connect', PDO::__construct);
                $this->setPDO('query', PDO::query);
                $this->setPDO('num_rows', PDOStatement::rowCount);
                $this->setPDO('fetch_assoc', PDOStatement::fetch);
                $this->setPDO('fetch_array', PDOStatement::fetch);
                $this->setPDO('result', PDOStatement::fetchColumn);
                $this->setPDO('free_result', PDOStatement::closeCursor);
                $this->setPDO('escape_string', PDO::quote);

                $this->initiated = true;

                $this->connect($_CONFIG['mysql']['connection_type']);
            }
        }

        final public function setPDO($key, $value)
        {
           
        }

    }

Not sure what to do for the: $this->MySQL[$key] = $value; part I feel like it should be a PDO fetch?


Maybe doing it that way is useless or pointless and I should just do it the correct way for PDO which is
PHP:
$host = 'localhost'; // MySQL host | Example: localhost or 127.0.0.1

$user = 'root'; // MySQL username / loging name | Example: root

$pass = 'Password'; // MySQL Password / leave blank if no PHPMyAdmin password | Example: 'Tester' or (if no password) ''

$db = 'add_database'; // MySQL database / schema

$port = '3306'; // MySQL port

$charset = 'utf8mb4'; //


$dsn = "mysql:host=$host;dbname=$db;charset=$charset;port=$port;";
$options = [
    PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
    PDO::ATTR_EMULATE_PREPARES   => false,
];
try {
     $pdo = new PDO($dsn, $user, $pass, $options);
} catch (\PDOException $e) {
     throw new \PDOException($e->getMessage(), (int)$e->getCode());
}
 

FirefighterKyle

I am Kyle!!
Sep 14, 2012
162
7
I would recommend using EasyDB by Paragonie. It makes using PDO a breeze.

-
Thanks for the tip, I will look into easydb.

@omatamix, so I am a little confused on how easydb works? I am not familiar with the composer way to install things I use Xampp so I feel lost trying to do it with another program. I tried to just download the files and put it into my htdocs and run it but I feel like it needs to be used with composer
 
Last edited:

omatamix

New Member
Feb 20, 2019
18
6
Thanks for the tip, I will look into easydb.

@omatamix, so I am a little confused on how easydb works? I am not familiar with the composer way to install things I use Xampp so I feel lost trying to do it with another program. I tried to just download the files and put it into my htdocs and run it but I feel like it needs to be used with composer
To get started with composer simply go to . Then click Download and find Download and run Composer-Setup.exe - it will install the latest composer version whenever it is executed. Then click Composer-Setup.exe. That will go through the installation process on your computer. Do not click developer mode, just click next. Then select you php engine from your xampp directory. For example, C:\xampp\php\php.exe. Then click next. Skip the proxy settings. And then finally click install and after it finishes click finish. Then open a new command prompt. Then navigate to your xampp htdocs folder. You can use commands like cd ../ to go back on directory or cd xampp to navigate to a folder. Then just type composer and hit enter.
You must be registered for see images attach

You should get something similar to above. Then in that same directory type composer require paragonie/easydb:^2 and hit enter, that will install EasyDB. Once it has finished composer will generate a composer.json and a composer.lock in your htdocs folder and a vendor folder to contain the code you just installed. Then you can access the code doing this.
PHP:
<?php declare(strict_types=1);

require __DIR__ . '/vendor/autoload.php';

// Then you can now use EasyDB.

$db = \ParagonIE\EasyDB\Factory::fromArray([
    'mysql:host=localhost;dbname=something',
    'username',
    'putastrongpasswordhere'
]);

$rows = $db->run('SELECT * FROM comments WHERE blogpostid = ? ORDER BY created ASC', $_GET['blogpostid']);
foreach ($rows as $row) {
    $template_engine->render('comment', $row);
}
?>
Composer can be used to install useful packages from other developers so you don't have to reinvent the wheel when building your web application. Composer has more stuff you can do, read their documentation .
Post automatically merged:

Also note some packages will require you to use a higher PHP version so if the package requires at least PHP 8 and you have PHP 7 composer will generate an error message. You can also learn more about the CLI commands here .
 
Last edited:

Users who are viewing this thread

Top