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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.