1

I'm making a school project on which I have to create a web app based on a Oracle database, and after testing some php frameworks I'm currently using CodeIgniter. I'm pretty new to the MVC and php, so excuse me if I make any mistake..

I understand that (beeing a MVC framework), on my Model, it's possible to run queries like

$query = $this->db->query("SELECT * FROM tablename");

But on my studies I read that it's good practice to, everytime we want to run a query/insert/etc, we should open a connection and end it after making the operation. I believe that's supposed to prevent 'inconsistencies'..Something like

<?php
    $conn = oci_connect('user', 'pass', 'dbname');
    $query = 'select * from tablename';
    $stid = oci_parse($conn, $query);
    oci_execute($stid, OCI_DEFAULT);
    while ($row = oci_fetch_array($stid, OCI_ASSOC)) {
    foreach ($row as $item) {
    echo $item." | ";
    }
    echo "
    \n";
    }
    oci_free_statement($stid);
oci_close($conn);
?>

Is that so? If it is, then the that command I'm making on my Model is correct or not? I can only think of running this php script on a view.php file but by doing so I guess I'm not implementing the MVC 'concept', just calling this kind of php script...right? I'm getting the results with the two method, but im not sure which's the best way to do it..

Thanks in advance!

2 Answers 2

2

You don't have do such loads of work. It's MVC. Mode-View-Controller . Everthing is all done and managed. You have to provide the connection details in database.php file kept in config folder. And ci will automatically make a connection with database.

And call these all queries in model. Make a function in model for your requirement and call it in controller. And, hence you can get your result in controller and pass it to the view.

Sign up to request clarification or add additional context in comments.

2 Comments

Yes, that's what I've done, I've set the connection details on database.php and I can run queries with the CI MVC syntax. I was just thinking that maybe if I don't make the whole open-connection/close-connection routine, when two users use the web app they could create some inconsistencies on the tables..I guess I'll read more about the Active Record as jtheman suggested and believe that the CI framework takes care of these kind of issues..
Hey, don't worry about how everything will be managed when you have even more than 10 users on website at one time. It all be managed and no consistencies will occur.
1

Since you are using the Codeigniter MVC framework then adding your own database routine would definitively be an overkill and not good practise. CI has a well developed and secure DB library and using CI Active record syntax greatly simplifies db queries. So look into this and learn the great benefits of a thought trough framework.

http://ellislab.com/codeigniter/user-guide/database/active_record.html

2 Comments

Thanks for your answer! On that page it says that "if you want to write your own queries you can disable this class in your database config file, allowing the core database library and adapter to utilize fewer resources."..Is that what I should do If I want to open and close connection with each database interaction?
Without digging too deep in the DB-class my best guess is that setting the value: $db['default']['pconnect'] = FALSE; in your file: application/config/database.php is going to achieve just that, that connection is closed after each interaction. Then you're free to use the active record syntax.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.