justlukeyou Posted June 23, 2013 Share Posted June 23, 2013 Hi, I have built my site in PHP but now I want to update it into PDO. However I have tried searching Amazon for PDO but nothing it coming based on PDO. Can anyone recommend a clear and easy to use book based on PDO please? Quote Link to comment Share on other sites More sharing options...
ginerjm Posted June 23, 2013 Share Posted June 23, 2013 If you've been using the MySQL extension, switching to pdo is not very hard. The manual lists all the functions and has sufficient examples to get you through the learning process. Save your money and play around for a bit. Quote Link to comment Share on other sites More sharing options...
justlukeyou Posted June 23, 2013 Author Share Posted June 23, 2013 Thanks, But I just dont understand the manual. I found this which looks useful: http://www.chrisjhill.co.uk/article/an-introduction-to-pdo-the-basics-and-benefits Quote Link to comment Share on other sites More sharing options...
ginerjm Posted June 23, 2013 Share Posted June 23, 2013 Then I'd go to a bookstore and peruse the selections there, rather than buy a pig in a poke from A. Quote Link to comment Share on other sites More sharing options...
justlukeyou Posted June 23, 2013 Author Share Posted June 23, 2013 Thanks, I live near a large town but the main book shops doesn't have many computer based books. When I google PDO it comes up with lots based on connecting to the database. A pro designer from Sweden told me that PDO is all about the connection. Thats why I didn't bother to learn it and focussed on PHP. Quote Link to comment Share on other sites More sharing options...
ginerjm Posted June 23, 2013 Share Posted June 23, 2013 Learning php is one thing. Using pdo for your db access is a completely different thing. Together you write data-based apps. PDO is just the piece that accesses the databases. There's a lot more PHP in an application than there is PDO. That's why a book is basically unnecessary. Here's an example of pdo usage: I have this standard script stored outside of my web-accessible server tree: I call it: pdo_db_connect_select.php and include it in all my db-needing scripts. <?php function PDOConnect($sc_dbname,$msg=null,$options=null) { // initialize // PDO requires it to be enabled in php.ini /* add this to the ini file: extension=pdo.so extension=pdo_sqlite.so extension=sqlite.so extension=pdo_mysql.so */ // Connect to mysql using pdo api $host="mysql:host=(your hostname);dbname=$sc_dbname"; $uid = "(your uid for your database)"; $pswd = "(password)"; Try { $mysql = new PDO($host,$uid,$pswd,$options); } catch (PDOException $e) { if ($msg == "ShowMsg") echo "Fatal Error<br>Failed to connect to mysql via PDO. PDO Error msg is:<br>".$e->getMessage(); else echo "Fatal Error<br>Possible bad dbname?<br>Failed to connect to mysql via PDO. Sensitive error msg may be viewed with additional parm to call to PDOConnect(dbname,'ShowMsg')"; return false; } if (!$mysql) { echo "Failed to connect to mysql via PDO. Error returned is: " . GetPDO_ErrorMsg($mysql); return false; } else return $mysql; } //***************************** function GetPDO_ErrorMsg($pdo,$i = 2) { $pdo_errinfo = $pdo->ErrorInfo(); return $pdo_errinfo[$i]; } (end) I included some extra code to make my life easier - you can eliminate or adopt it. *!*!*!**! As for actually using PDO here is a simple approach. Note - I'm showing you simple query usage, not a prepared query situation. Start with this and graduate later. // database access require(($your_private_path)."pdo_db_connect_select.php"); $pdo = PDOConnect((a dbname)); $q = "select driver_name,driver_season from drivers"; $qrslts = $pdo->query($q); $numrows = $qrslts->rowCount(); echo "Found $numrows records<br>"; if (!$qrslts) { echo "Could not gather driver names - Error msg is: ".GetPDO_ErrorMsg($pdo); exit(); } else { while($row = $qrslts->fetch(PDO::FETCH_ASSOC)) { (your php handling code here } } (end) This code shows you how to include your std. connection code in your script, how to run the query, handle errors (using again, a simple function I added which you don't have to use), and process the results. Again - you are going to get a few posts (lots?) saying you should use prepared queries. For one-shot queries and scripts doing proper input validation, I find prepared queries not necessary, but they do have a use and you will need to move towards learning them later on. Hope this clears up your confusion. Good luck. Quote Link to comment Share on other sites More sharing options...
gizmola Posted June 23, 2013 Share Posted June 23, 2013 Thanks, But I just dont understand the manual. I found this which looks useful: http://www.chrisjhill.co.uk/article/an-introduction-to-pdo-the-basics-and-benefits That article does an excellent job walking you through the basics of PDO. I'm not sure what else you would need. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.