0

i am not new to PHP but definitely beginner in MVC stuff. I have read about MVC and get understanding that Model handles all DB operations ie. CRUD

I googled bit nd found Model.php as

class Model {

private $connection;

 public function __construct()
 {
    global $config;

    $this->connection = mysql_connect($config['db_host'], $config['db_username'], 
                        $config['db_password']) or die('MySQL Error: '. mysql_error());
    mysql_select_db($config['db_name'], $this->connection);
 }

 public function execute($qry)
 {
    $exec = mysql_query($qry) or die('MySQL Error: '. mysql_error());
    return $exec;
 }

}

Now, i want to replace __construct() with PDO and also change function execute($qry) with PDO.

As of now i have included PDO.php class with DB connection as require_once in index.php but in this way i feel that Model is not getting used at all.

I am using PIP framework for PHP to use MVC. Please let me know how can i use PDO here. Link to PIP framework is HERE

Gags
  • 3,759
  • 8
  • 49
  • 96
  • maybe interesting? [a simple PDO wrapper that can let you to use PDO most simple and secure way](http://stackoverflow.com/a/33069595/3184785). Converting the PIP model to use this logic would not be difficult. The biggest change is that the execute method would have two parameters. 1) the SQL query. 2) The parameters array to be bound to the placeholders in the query. It would make the database queries quite safe. – Ryan Vincent Feb 13 '16 at 05:03
  • Better late than never? Was interesting... – Ryan Vincent Feb 17 '16 at 12:53
  • Website and code are available: Website [Using PDO in PIP MVC](http://rfv123.eu.pn/pip/), it has the links to the source code. Just in case: [The original PIP with PDO added](http://ge.tt/4ZiGr5Y2). Also: Website: [check 'data' directory for the Database Layout / data](http://ge.tt/6ZZgr5Y2). I will amend the `Readme` with the new features if required but the code is documented and shows valid use. – Ryan Vincent Feb 17 '16 at 16:23
  • Hello @RyanVincent. I really apologise for not replying your comment as i was not available on internet. I checked your approach and it is fabulous. However can be refined in some or other way but really gud stuff. I really appreciate your effort. Please post last answer as answer so that this post can be useful for other users also. Also pl share your mail id so that we can discuss directly the refinements or some other stuff technically. – Gags Feb 20 '16 at 15:18
  • Last line of my profile has a working email :) Glad you found the code useful. I will use it for simple websites. Looking forward to hearing about the improvements you suggest to it. – Ryan Vincent Feb 20 '16 at 15:47
  • Yup for simple websites. It is very useful. – Gags Feb 20 '16 at 15:58
  • 1) I don't see much point in posting an answer as it is of minimal interest to others. I enjoyed doing it for me. 2) I really would like your thoughts on what to change and what to add to 'PIP'. What is confusing with the update? What do you want to add? If it is easy - we can add it :) My email is in my profile. Others have used it. Post a comment here and I will provide a clear email to contact me. – Ryan Vincent Feb 25 '16 at 19:05
  • For myself, I will add 1) a 'class auto loader', It is too useful not to have it. 2) I would consider replacing the current 'routing' with a simple 'router' component as it gives a lot more flexibility for free. With the 'auto loader' comes access to logging / components etc. as required. I have no issues with the `models` and `views` which work. – Ryan Vincent Feb 25 '16 at 19:17
  • yes.. @RyanVincent .. you got it right. We should add 1) autoloader class for PHP files and CSS,JS 2) PHP script to combine and minify CSS & JS 3) we should add template folder '.tpl' files. 4) As you suggested about Router component is also a good idea. Will you incorporate your changes and then after we can rectify more. :) – Gags Feb 26 '16 at 03:16
  • Would you like to share that version with all these features ? It will be great to have a look at it. – Gags Mar 08 '16 at 18:02

0 Answers0