0

I understand that mysql_* is in the process of being removed from future PHP versions, probably not for some time, but I want to migrate my site.

I'm currently relying on the mysql functions to communicate with my database.

I want to create a set of functions dedicated to the PDO interface (PDO Interface Manual)

I just have no ideas on where to start, and if my plan will actually work.

Examples:

   <?php
  function PDOFetch($Var)
{
$sth = $dbh->prepare("$Var");
$sth->execute();
$result = $sth->fetchAll();
return $result; 
}

function PDONumb ($Var)
{
$Query = $dbh->prepare("{$Var}");
$Execute->execute();
$count = $Execute->rowCount();
return $count;
}
?>

if I have a row of constant connections to the functions For example:

<?php 
include "PHP/PDOFunctions.php"; 

$UserArray = PDOFetch("SELECT * FROM users WHERE username='$username'");
$Password = $UserArray['Password'];
$UniqueID = $UserArray['UID'];

$ContactInfo = PDOFetch("SELECT * FROM ContactInformation WHERE UID='{$UniqueID}'");
?>

Would the above solution be a time effective and reliable solution to put in place on my webserver?

I understand there may be errors with the code, but i'm completely new to the PDO API and want a nice script to handle my entire queries

Thanks in Advance

Daryl Gill

Daryl Gill
  • 5,464
  • 9
  • 36
  • 69
  • Since you are using PDO you should start using [prepared statements](http://j.mp/T9hLWi). Your current code is probably vulnerable to [SQL injection](http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection). If you need help see this decent tutorial: http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers. Also you really have to wonder whether you really need to build something around PDO, because the API as is is pretty easy to work with. – PeeHaa Nov 30 '12 at 22:41
  • The API May be easy to work with, but I want to minimize the lines of PHP on my main pages, and serve out to the main bits in external functions; What i'm trying to get at, If I secure from SQL Injection would this solution work? – Daryl Gill Nov 30 '12 at 22:45

1 Answers1

1

I have a simple-ish PDO wrapper class that I've been using for ages, I've just thrown it up on my blog so it's easier [for me] to copy/paste around.

http://sammitch.ca/2012/11/simple-php-pdo-wrapper-class/

Even if you want to roll your own solution, it may help you in the right direction.

Sammitch
  • 30,782
  • 7
  • 50
  • 77