Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / NewStats: 3,195,530 members, 7,958,624 topics. Date: Wednesday, 25 September 2024 at 06:55 PM |
Nairaland Forum / Science/Technology / Programming / Hello House: Let's Do A Little PHP Using PDO (6356 Views)
Login And Regisatration In Php Using PDO / How Do I Retrieve Data With Apostrophe From Mysql Using PDO / If You Want To Learn Angularjs With Php..using XAMP ...why Not Join In Here (2) (3) (4)
Hello House: Let's Do A Little PHP Using PDO by FrankLampard: 3:11pm On Feb 29, 2016 |
For those of us who find it hard to comprehend OOP PHP especially PDO. These few texts of mine would be great for you. First, let us know What is PDO PDO is a PHP Extension which basically stands for PHP Data Objects (PDO). With PDO developers can create applications that can connect across multiple database with little modification to their PHP code. When connecting to MySQL database PDO uses PDO MySQL driver for it's connections. To know all the database that your PHP PDO server supports. simple run the following PHP code:
If you have been using using mysql_connect for connection to your database I advice you stop doing so and switch to this awesome driver called PDO which have been tested in terms of performance with other PHP drivers like MySQLi and MySQL and has been proven worthy. Let us Look at some of the ways you can connect to your database using PDO When I ran the code above in my localhost using WAMPSERVER 2.5 that has PHP 5.5.12 installed, only MySQL and SQLite were supported, so I will be doing some database connection using only this two database. But according to PHP, PDO supports several database ranging from
So let's see the code for database connection to MySQL and SQLite on PDO MySQL
You can see how Exception is being handled in case we encounter some unknown error while trying to connect to your database (Java and Android guys will understand this wella ). Exception might be handled when the server is quite overwhelmed. $e->getMessage()will display the error to your browser as a String. Connection to SQLite DB
This simply connects to the path where your SQLite DB file is located in your local machine. By doing all those up there, let's look at some of the PDO Queries INSERT
From the code you can see it requires less code to perform an operation. In PDO to close a database connection is as simple as $conn = nullon your connection variable SELECT SELECT statement in PDO is quite easy. Let say we have a table called "nl_programming_gurus" CREATE TABLE nl_programming_gurus ( id MEDIUMINT( NOT NULL AUTO_INCREMENT PRIMARY KEY, you can do your database SELECTING in PDO like this
The following SELECT statement will give a result below Connected to Database You will have noticed that I can iterate over the result set directly with foreach. This is possible with PDO So let's do the UPDATE STATEMENT
The above code will UPDATE the second record with an id of 2 with FrankLampard DELETE STATEMENT
The above code will delete the record in the database with an id of 2. FETCH Modes The section above showed how using PDO::query we can fetch information from the database. The PDO::query method returns a PDOStatement object that can be utilized in much the same was as mysql_fetch_object(). Of course there are times when an numerical index is needed or an associative index. PDO::query provides for this also by allowing the coder to set the fetch mode for via the PDOStatement object or via PDOStatement::setFetchMode(). I will list some of the FETCH Modes used in PDO. FETCH ASSOC To fetch an associative array from our results the constant PDO::FETCH_ASSOC is used and returns the column names as indexes or keys of the resulting array. FETCH NUM Like PDO::FETCH_ASSOC, the PDO::FETCH_NUM produces a numerical index of the result set rather than the field names. FETCH BOTH There may be times you need to fetch both numerical and associative indexes. PDO::FETCH_BOTH produces a numerical and associative index of the result set so you can use either, or both. I know this texts is boring to some, but let just talk about one of the MOST IMPORTANCE aspect of PDO which is Prepared statements Prepared statements basically compiles your SQL statements and performs query optimization on the SQL statement, and stores the result without executing it. It helps in preventing SQL Injection. PDO accepts two kinds of parameter markers. named - :name question mark - ? You must choose one or the other, they cannot be mixed. Lets dive in and have a look at how PDO::prepare and PDOStatement::execute work together.
PDO gives you some level of security, but you need to still sanitize your input and escape your output. Lastly and finally, let's see how we can get our lastId when performing transactions Getting Last Insert Id This is a common task required when you need to get the id of the last INSERT. This is done with PDO::lastInserId() method as shown below. <?php The above code spinet will out the last Id transaction on your database. That is it. PDO is a very nice wrapper/driver, for me I see it as a Framework on its own. Hope this series of texts would be helpful. Thank you. If you feel I made any mistake feel free to correct me. 3 Likes |
Re: Hello House: Let's Do A Little PHP Using PDO by ivcraig(m): 4:38pm On Feb 29, 2016 |
Thanks for taking out time to do this for all who want to transit from function-oriented (procedural) PHP programming to object-oriented. I have become very much accustomed to mysqli procedural. But please I need to know why switching to PDO for database operations is advantageous, apart from PDO's power to connect to other databases? Thank you. |
Re: Hello House: Let's Do A Little PHP Using PDO by FrankLampard: 6:02pm On Feb 29, 2016 |
ivcraig: Just like I said in the tutorial. PDO has been tested in terms of performance and has proved worthy. Check this URL. This guys explained it all. http://wooptoo.com/blog/pdo-vs-mysqli-performance-comparison/ PDO has prepare statement. which gives your app some level of security. |
Re: Hello House: Let's Do A Little PHP Using PDO by DonSegmond(m): 9:04pm On Feb 29, 2016 |
Or just read php's PDO manual at http://php.net/manual/en/book.pdo.php |
Re: Hello House: Let's Do A Little PHP Using PDO by ivcraig(m): 9:12pm On Feb 29, 2016 |
This benchmark is flawed, as PDO does escaping/preparing entirely on the client-side, while MySQLi uses the mysql protocol for that (thus network IO). The writer even agreed to it himself. Read the end of the article. |
Re: Hello House: Let's Do A Little PHP Using PDO by FrankLampard: 6:12am On Mar 01, 2016 |
ivcraig: Where are you driving at? |
Re: Hello House: Let's Do A Little PHP Using PDO by ivcraig(m): 9:08am On Mar 01, 2016 |
FrankLampard:My point is PDO seems to have only one advantage, which is being able to connect to other databases. |
Re: Hello House: Let's Do A Little PHP Using PDO by paranorman(m): 9:08am On Mar 01, 2016 |
Oga, FrankLampard, how about MS-MQL na? And again, can you put me through how I can connect PHP to MS Access DB? XAMPP'S my development server. |
Re: Hello House: Let's Do A Little PHP Using PDO by FrankLampard: 9:52am On Mar 01, 2016 |
ivcraig: No, No, No, Did you read that URL I posted earlier. PDO outwit MySQLi in term of performance. We can't even talk of MySQL because that one is depreciated. PDO::PreparedStatement gives your application some level of security. There was a small App I built with PDO recently, I tested for SQL injection and it passed the test. Without even writing any SQL escape functions. WARNING: YOU HAVE TO STOP USING PROCEDURAL PHP, BECAUSE PHP 7 DOES NOT HAVE SUPPORT FOR IT SHA SHA |
Re: Hello House: Let's Do A Little PHP Using PDO by FrankLampard: 10:05am On Mar 01, 2016 |
paranorman: Have you ran the first code spinet in my article to check if your current XAMP server have support for MS Access DB, because when I did it in my my WAMP server only MySQL and SQLite was supported, that was why I didn't show you guys the connection code spinet for database connection, but according to PHP, he is the code spinet for connection to MS Access Database. NB: I didn't test this code whatsoever.
|
Re: Hello House: Let's Do A Little PHP Using PDO by paranorman(m): 10:17am On Mar 01, 2016 |
FrankLampard:thanks. What if it does not support it, how can I go about it? Isn't this a driver thing? |
Re: Hello House: Let's Do A Little PHP Using PDO by FrankLampard: 11:28am On Mar 01, 2016 |
paranorman: You have to go to your php.ini file to enable it, if it is not supported by default. |
Re: Hello House: Let's Do A Little PHP Using PDO by paranorman(m): 11:33am On Mar 01, 2016 |
FrankLampard:thanks, I already did that. |
Re: Hello House: Let's Do A Little PHP Using PDO by FrankLampard: 2:24pm On Mar 01, 2016 |
paranorman:Looking forward to seeing how you used PDO in developing Enterprise Solutions. |
Re: Hello House: Let's Do A Little PHP Using PDO by paranorman(m): 3:15pm On Mar 01, 2016 |
FrankLampard:will get there, someday. So, what lang(s) are you a pro in? |
Re: Hello House: Let's Do A Little PHP Using PDO by FrankLampard: 3:30pm On Mar 01, 2016 |
paranorman: My first love is Java, recently started doing PHP. 1 Like |
Re: Hello House: Let's Do A Little PHP Using PDO by ivcraig(m): 9:14pm On Mar 01, 2016 |
FrankLampard:The author in the article you sent me said his test result is wrong. I suggest u also read it again. |
Re: Hello House: Let's Do A Little PHP Using PDO by FrankLampard: 11:48pm On Mar 01, 2016 |
ivcraig: Digest the first paragraph first. What he meant was that MySQLi was supposed to be better than PDO, but using his benchmark test PDO surprisingly outwit MySQLi. |
Re: Hello House: Let's Do A Little PHP Using PDO by nnamdiosu(m): 12:29pm On Mar 02, 2016 |
thanks bro. really appreciate the tutorials. God bless |
Re: Hello House: Let's Do A Little PHP Using PDO by FrankLampard: 12:35pm On Mar 02, 2016 |
nnamdiosu: Thanks |
Re: Hello House: Let's Do A Little PHP Using PDO by ivcraig(m): 1:29pm On Mar 02, 2016 |
The conclusion of the writer after the test.
|
Re: Hello House: Let's Do A Little PHP Using PDO by FrankLampard: 2:30pm On Mar 02, 2016 |
^^^ It seems you have made up your mind on the procedural route. Even tho I have explain this countless number of times. |
(1) (Reply)
I Want To Become A Good Systems Programmer Before I Graduate / Help A Frustrated Programmer With Power Problem / Please Review My Personal Website
(Go Up)
Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health religion celebs tv-movies music-radio literature webmasters programming techmarket Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10) Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 74 |