Archive for the ‘Tips’ Category

PHP PDO MySQL – Simple Example Connecting to MySQL With PDO Class

I’ll demonstrate a simple example on how to connect to MySQL using PHP’s PDO class. Just some of the benefits of PDO is that it’s fast and if you use the PDO::prepare() method it will prevent SQL injection attacks by calling the PDO::quote() method. The other pros is that there are several databases it will support. So let’s dive right into the code:

$ hostname = ‘localhost’;
$ username = ‘your_username’;
$ password = ‘your_password’;

try {

$ db = new PDO(“mysql:host=$ hostname;dbname=mysql”, $ username, $ password);

echo ‘Connected to database’;
}
catch(PDOException $ e) {

echo $ e->getMessage();
}

Fatal Error new PDO Instance

Just an important note that if you receive the following type of fatal error in your development environment:

Fatal error: Uncaught exception ‘PDOException’ with message ‘SQLSTATE[42000] [1049] Unknown database ”user”’ in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\test\trunk\code\login\classes\std.pdo_singleton.class.inc:30 Stack trace: #0 C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\test\trunk\code\login\classes\std.pdo_singleton.class.inc(30): PDO->__construct(‘mysql:host=loca…’, ‘username’, ‘password’) #1 C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\test\trunk\code\login\classes\std.mysql.class_test.inc(43): db::getConnect() #2 C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\test\trunk\code\login\connect.php(6): MySqlDb->confirmUserPass(‘usertest’, ‘passtest’) #3 {main} thrown in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\test\trunk\code\login\classes\std.pdo_singleton.class.inc on line 30

Looks pretty messy and hard to decipher.

When trying to decipher error code I usually look at the first error which led me to see why it was reporting an “Unknown database” when it was existing. The extra quotes also gave me a hint as to the problem. So I concluded the problem resulted in the placement of extra quotes around the values of host and/or dbname. The following will generate the previous error:

$ db = new PDO(“mysql:host=’localhost’;dbname=’mysql’”, $ username, $ password);

So if you do not use variables then do not add the single quotes for the values of host and dbname. In other words, use the following code instead:

$ db = new PDO(“mysql:host=localhost;dbname=mysql”, $ username, $ password);

This is a simple test to connect to your mysql database or any other support database. Just for your information if you wish to connect to PostgreSQL which is another popular and robust database use the following code in place of the line of instantiation:

$ db = new PDO(“pgsql:dbname=pdo;host=localhost”, “username”, “password” );

If you’re in a development environment and wish to display your errors directly to the screen you can specify the errors displayed. You need to set your display_errors settings to ‘on’ in your php.ini file. So set your error attributes after instantiating the PDO class like so:

$ db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

There are three types of error report settings for PDO::ATTR_ERRMODE:
PDO::ERRMODE_SILENT = error codes
PDO::ERRMODE_WARNING = E_WARNING
PDO::ERRMODE_EXCEPTION = Throw exceptions

Here’s an example of implementing PDO::ATR_ERRMODE:

$ hostname = ‘localhost’;
$ username = ‘your_username’;
$ password = ‘your_password’;

try {

$ db = new PDO(“mysql:host=$ hostname;dbname=articles”, $ username, $ password);

echo ‘Connected to database’; // check for connection

$ db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);

$ sql = ‘Select * from tutorialref where id=1′;

$ result = $ db->query($ sql);

foreach ($ result as $ row) {

echo $ row['id'] .’ – ‘. $ row['author'] . ‘
‘;

}

$ db = null; // close the database connection

}
catch(PDOException $ e) {

echo $ e->getMessage();
}

Don’t confuse this with the errors generated from the php setting of error_reporting. The errors from PDO::ATTR_ERRMODE apply to the sql query and its results. I’ll dive into the error settings and the different outputs of the php.ini error_reporting settings and PDO::ATTR_ERRMODE report settings in a future article.




Shop With Bistromd Coupons, Bistromd Coupon Codes

of purchasing at BistroMD online store. There are many companies who offer BistroMD coupons, BistroMD discount codes and BistroMD rebate codes for free, among them Cupons4discount.com is said to be the best and it also free for using BistroMD coupons and BistroMD coupon codes. So visit Cupons4discount.com every time before you step forward to shop online irrespective of online store.
Coupon Codes
coupons4discount.com

BistroMD coupon codes were an idea which originated several decades ago when the retail business really began to grow to extremely large proportions. BistroMD coupons and BistroMD coupon codes made many people life easier and BistroMD coupons and BistroMD coupon codes are also easy to use, indeed simple. Customers are looking for BistroMD coupons and BistroMD coupon codes in order to save money at BistroMD online store. Cupons4discount.com can help in finding BistroMD coupons and BistroMD discount codes. Visit Cupons4discount.com on daily basis in order to get best and non-expired coupons.Coupon Codes
coupons4discount.com

Many people who shop online at BistroMD know the value of BistroMD coupon codes and BistroMD Discount codes because they know that these BistroMD discount codes are only offered to customers who are savvy enough to find the codes and save them for future usage at BistroMD online store. So the reason Cupons4discount.com was formed in order to provide BistroMD coupons and BistroMD coupon codes to online shoppers for free. Cupons4discount.com is free coupon website for any body who dream about saving money while shopping online.Coupon Codes
coupons4discount.com

For online shopper who purchases often from the Internet and from BistroMD should use BistroMD coupons and BistroMD discount codes for saving money with discounts. Many online coupon sites post update-to-date BistroMD coupon codes and BistroMD Discount codes for you to choose from. Cupons4discount.com is one among such sites here at Cupons4discount.com they update BistroMD coupons and BistroMD Discount codes on daily basis. That is just awesome. So why wait visit Cupons4discount.com and find BistroMD coupons and coupon codes and start saving your money.




Volkswagen Cars: New Beetle and Jetta

Welcome to the luxurious journey of Volkswagen. People all know that Volkswagen Cars are ultimate style and comfort because the interiors and the exteriors features are unmatched in its range. You can hardly find anyone driving Volkswagen Cars in India. Only the affluent class of people will afford to drive it.

 

Some models from Volkswagen are Volkswagen Jetta, Volkswagen New Beetle, Volkswagen New Polo, Volkswagen Passat, Volkswagen Phaeton, Volkswagen Vento and Volkswagen Touareg. These elegant models of Volkswagen Car are available in the range of Rs 4, 34,000 to Rs 76, 29,128.

 

Let’s talk about the two most favorable and superb brands such as Volkswagen Jetta and Volkswagen New Beetle.

The people who want to maintain their status in the society will buy these Volkswagen Cars like rich class people. As the prices of these cars are so high then of course they are embedded with the mind blowing features.

 

New Beetle Car is designed so well and its outer appearance will surely amazed you. The materials used in this car are carefully tested and have outstanding quality. It’s true that New Beetle is the small car but when the safety comes, it gives you the feeling of security at the time of driving. New Beetle car is equipped with all safety features like ESP, ABS, four airbags, 3-point inertia reel seat belt system at front and rear. Without taking much tension enjoy your drive with enthusiasm.

 

When it comes to comfort and convenience the car is loaded with all specifications that will surely gives you the drastic and thrilling drive.

New Beetle car included the features like electrically adjustable and heated door mirrors, power steering, color-keyed door mirror housings, door handles and bumpers and, of course, the legendary flower vase.

 

The Jetta was introduced in the year of 2008 in India. This Volkswagen Jetta car design, looks and the feel of this car would leave you astonished. The interior features of the car is embedded with Power windows with pinch protection, 60/40-split folding rear seats, Audio Sound system with MP3, 15.5 cubic feet of trunk space, air conditioning (climatic), central locking with RF remote.

 

The exterior of the car includes Carefree Maintenance Program, Electronic Stability Control 6 airbags, Tire Pressure Monitoring System. Volkswagen Jetta car is powered by powerful engine and gives excellent fuel mileage. The car is available in both versions Petrol as well as Diesel.

 

Desire the luxurious drive then go with these two elegant cars.

 




Garmin Nulink 1695 GPS

World-wide Positioning System (GPS) is the main device you need to carry during the journey, if you might be visiting a new place which you have never went to. This positioning device contains various models in addition to brands, and this year, there are a few new GPS device. One of the best recommended GPS by the end of this 2010 will be the new garmin nulink 1695. This can be a brand new GPS NAVIGATION from nuLink family that mainly gives you high technology characteristics.

This new Garmin Nulink 1695 device offers wide monitor which are rarely found practically in GPS from the prior years. Indeed, prepared by 5″ monitor, the following device obviously delivers highly interactive in addition to detailed view.

There are plenty of factors that help make nuLink 1695 the very best GPS of all seasons, and one of them is Google Area Search feature. This local search permits you to easily find any places for example area. If you might be driving in California and you have ever saw this state previous to, and you really need to want to stay in the medium-class hotel, this local search will allow you to find one.

Using the local search characteristic offers by Search engines, in other text, will somewhat give you an experience on the local travel. Usually there are some others high-tech features in this GPS device; they’re weather, fuel charges, flight status, NAVTEQ traffic, and much much more. There is also a feature on Nulink 1695 that permits you to plan your take a trip. This route prepare will sort various destinations, so you’ll be able to choose any route you would like.




Customize Your Television Service with Satellite TV

Consumers in the United States know what they want with regard to services and products.  The internet has provided them with the opportunity to see all that is offered, get available options and argue prices.  Companies and manufactures have needed to respond to the increased power of the consumer and buyer with the available technology of the internet.  The internet has given the buying power back to the consumer, and companies providing services such as satellite television have responded by offering customized service.

Customized packages exist for all types of viewers, from the young to the old, singles and families.  No matter what your preference is, you will be able to find exactly what you are looking for regarding television services with satellite TV.  You can find everything from sports, movies, sitcoms, documentaries, local channels, and even an international selection.  The possibilities are endless.

Satellite television companies understand the importance of its clients to want customized programming with their television services.  The fact of the matter is that there are potentially hundreds of channels available to watch, yet customers do not have the energy, the desire, or the time to enjoy them all.  By customizing your television package, you can select and personalize your programming options.

 

Available options with satellite packages include selecting the number of channels you wish to receive.  You can get as few as 150 channels or up to almost 300, including local channels from your area.  First, simply select the basic package you want to receive, and there is no need to worry as you can customize the package as you go along to include whatever programming or channels you wish to include.

In order to further entice potential clients, satellite companies further offer additional incentives to its clientele.  This includes free HD or DVR receiver upgrade for different packages, and some even include free HD for life.  Imagine the possibilities available with satellite television service.  Unlike basic cable programming, which preselects programming for its clients, satellite gives the control to its customers.  

After customers select their basic package option, they can customize their service by choosing different channels such as sports stations, movie channels, and even international programming.  This feature is especially appreciated by families, businesses, and those living with roommates.  Families incorporate people of different ages and tastes with television viewing.  The kids want to watch more cartoons and children programs, while mom and dad would prefer movies and adult programming.  

Roommates living together come in all shapes and sizes.  Co-ed living arrangements especially are difficult with regard to television programming.  The guys typically want to watch their sports while the girls like the fashion shows and reality television.  Thanks to satellite television, it can all be available to watch as part of the same package.  For one low monthly cost, customers can get exactly what they want with television.  

There is no need to worry about not receiving basic channels such as ABC Family, MTV, Discovery Channel, and CNN.  They are all available with every package, along with over a hundred more.  People know what they want with regard to television service.  Those that want to customize their own service and get the best on the market should consider switching to satellite television.