Zend Framework offers a very robust solution for almost any kind of web site; however, there are times when all you want to do is connect to a database without the need for the rest of the framework. Bellow I will show a quick example of how to connect to two different databases using the Zend_Db, Zend_Config_Ini, and Zend_Loader classes.
First you need to define your ini file (make sure to store this file out side of the web root, mine is stored at /var/)
[production] ; PostgreSQL database connection info db.pgsqlServer.adapter = PDO_PGSQL db.pgsqlServer.config.host = server_ip_or_url db.pgsqlServer.config.dbname = database_name db.pgsqlServer.config.username = "dbuser" db.pgsqlServer.config.password = "dbpassword" db.pgsqlServer.config.driver_options.PDO::ATTR_ERRMODE=PDO::ERRMODE_EXCEPTION ; Microsoft SQL Server database connection info db.mssqlServer.adapter = PDO_MSSQL db.mssqlServer.config.pdoType = dblib db.mssqlServer.config.host = server_ip_or_url db.mssqlServer.config.dbname = database_name db.mssqlServer.config.username = "mssql_user" db.mssqlServer.config.password = "mssql_password" db.mssqlServer.config.driver_options.PDO::ATTR_ERRMODE=PDO::ERRMODE_EXCEPTION
From the above ini file contents, there are a few things to take note of:
- You need to define the pdoType as dblib when connecting to MSSQL with the PDO Adapter (the default type mssql is not available in the php5_sybase package)
- You can set driver_options (attributes) for the PDO connection, some of the attributes you can set are
- PDO::ATTR_ERRMODE = PDO::ERRMODE_EXCEPTION (throw exceptions)
- PDO::ATTR_PERSISTANT = true (make persistent connections)
- PDO::ATTR_DEFAULT_FETCH_MODE=PDO::FETCH_ASSOC (return an associative array when fetching query result by default)
Next, create your php file that will be using the database connections. For this example, my php file is located at /var/www/example_project and the Zend libraries are located at include/libs/Zend
<?php //set the include path for Zend Framework (the include path is not defined in php.ini) set_include_path('include/libs' . PATH_SEPARATOR . get_include_path()); //Include the Zend_Loader class require_once 'Zend/Loader.php'; //load the Zend classes for use Zend_Loader::loadClass('Zend_Config_Ini'); Zend_Loader::loadClass('Zend_Db'); //load the configuration file $config = new Zend_Config_Ini('/var/my_example.ini', 'production'); //try to connect to and work with the postgres database try { //connect to postgresql $dbh = Zend_Db::factory( $config->db->pgsqlServer->adapter, $config->db->pgsqlServer->config->toArray() ); //set the instructor id number to use for the query $instructorID = 1234; //define the query $sql = "SELECT Full_Name From Trained WHERE train_id = :idNumber"; //prepare the query $stmt = $dbh->prepare($sql); //bind the instructor id as an integer data type to the query $stmt->bindParam(':idNumber', $instructorID, PDO::PARAM_INT); //execute the query $stmt->execute(); //if an instructor with the given id exits if ($stmt->rowCount() > 0) { $row = $stmt->fetch(PDO::FETCH_ASSOC); echo "The instructors full name is: ",$row['full_name']; } //clear the statement $stmt = null; //close the database connection $dbh->closeConnection(); } catch (PDOException $e) { //echo $e->getMessage(); echo "There was an error querying the database"; } catch (Zend_Db_Adapter_Exception $e) { //echo $e->getMessage(); echo "Unable to connect to the database"; } catch (Exception $e) { echo $e->getMessage(); } ?>
In the above example, I only connected to and used the Postgres database since the code for using either database is almost exactly the same. The only thing you would need to change in the above code to use the MSSQL database would be to change the pgsqlServer part in the Zend_Db::factory() parameters to mssqlServer.
In closeing the catch blocks at the bottom the php file do the following
- PDOException – Catches any exception thrown by PDO while working with the database (in this example, this pertains to querying the database)
- Zend_Db_Adapter_Exception – Catches any exception thrown while trying to connect to the database (or while using Zend_Db_Adapter, the $dbh variable)
- Exception – Catches any other exceptions I missed
It’s really a cool and useful piece of information. I am happy that you shared this useful info with us. Please keep us informed like this. Thanks for sharing.
Magnificent goods from you, man. I’ve understand your stuff previous to and you’re just too excellent.
I really like what you’ve acquired here, certainly like what you’re saying and the
way in which you say it. You make it entertaining
and you still take care of to keep it smart. I can not wait to read much more
from you. This is actually a great site.
Thank you. I’m glad you like it.
What’s up to every one, the contents present at this site are actually remarkable for people experience, well, keep up the good work fellows.
Hey there! Quick question that’s completely off topic. Do you know how to make your site mobile friendly? My site looks weird when browsing from my apple iphone. I’m trying to find a template or plugin that might be able to correct
this issue. If you have any suggestions, please share.
Appreciate it!
There are a couple of frameworks that can help you with that. One is Twitter Bootstrap Framework which allows you to create different layouts for different screen sizes. Check out the “Responsive Design” section here http://twitter.github.io/bootstrap/scaffolding.html#layouts
One of the frameworks my work has looked at mobile presentation is MWF (mobile web framework) http://mwf.ucla.edu/
If I remember right is used some CSS screen size checking to determine how to display.
Thanks on your marvelous posting! I really enjoyed reading it, you happen to be a great author.I will be sure
to bookmark your blog and will eventually come back very soon.
I want to encourage that you continue your great writing, have a nice weekend!
Good site you have got here.. It’s hard to find quality
writing like yours these days. I truly appreciate individuals like you!
Take care!!
Do you mind if I quote a couple of your posts as long
as I provide credit and sources back to your site?
My blog site is in the very same niche as yours and my visitors would genuinely
benefit from a lot of the information you provide here. Please let me know
if this ok with you. Many thanks!
Yeah, that would be fine with me
Јe suus tout à fait en accord avec vous