Jump to content

Best Books for PDO?


justlukeyou

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/279488-best-books-for-pdo/#findComment-1437566
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/279488-best-books-for-pdo/#findComment-1437569
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/279488-best-books-for-pdo/#findComment-1437575
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.