Hello there!

I am new with PDO and I am trying to replace the old code with PDO...

The main issue i have is that i dont know how to get/use the objects vs the arrays when it comes to showing data.

Please see bellow examples and let me know if you can help.


class customer 
    function getCustomer($customer_id) 
        $Sql = "SELECT * FROM customer WHERE customerID=".mysql_real_escape_string($customer_id);
        $Res = @mysql_query($Sql);
        $Row = @mysql_fetch_array($Res);
        $this->Name 		= ucfirst(($Row['Name']));
        $this->Email 		= ucfirst(($Row['Email'])); 
    return $Row;

and in order to use the class i do the following:

//initiate the class
$Customer = new customer;
$Customer -> getCustomer($_GET[customer_id]);
echo $Customer->Name;
echo $Customer->Email;

i also use the code this way when i need forms:

<input name="customer_name" type="text" value="<?=$Customer->Name?>" />


I am learing PDO and i am trying to replace the old code (like the one above) with the new one, but i am not sure i am doing the right things...please see bellow:


//db coonection
$db = new PDO("mysql:host=$db_host;dbname=$db_name",$db_user,$db_pass);
class customer 
	function __construct($db)
	  	$this->db = $db;

    function getCustomer($customer_id) 
        $Sql 	= "SELECT * FROM customer WHERE customerID=".mysql_real_escape_string($customer_id);
		$rs		= $this->db->query($sql);
		//$row 	= $rs->fetch(PDO::FETCH_ASSOC);
        $row 	= $rs->fetch(PDO::FETCH_OBJ);
    return $row;
//initiate the class
$Customer = new customer;

1-how can i use the Name and Email as the example shown above?

could I the following?

//initiate the class
$Customer = $Customer -> getCustomer($_GET[customer_id]);
echo $Customer->Name;
echo $Customer-Email;

How do you guys use it?
Sorry if i am not very clear! :)

Recommended Answers

All 3 Replies

Member Avatar

I use a pdo wrapper class form here:


You don't need to use mysql_real_escape_string with is as it uses parametric binding.

The documentation is a little thin, but adding it to a project is straightforward. The setErrorCallbackFunction is a bit of a fiddle to get working though.

this code is already a loop over

$row 	= $rs->fetch(PDO::FETCH_OBJ);

meaning, the objects can be called like this

echo $row->Name; and echo $row->Email;

However, if those objects are needed to be called through the instantiation of the class without being printed or echoed within the class, it may cost a little bit of server resources. Memory wise, but not a lot :).

We need to pass the obj to something else that we can call during the class instantiation. Not the best choice, but this is all I can think of right now.

Let's expand your code a little

## remember this is a loop over
$row = $rs->fetch(PDO::FETCH_OBJ);

## let's assign them
$item['name'] = (string)$row->name; 
## Warning name is case sensitive which pertains to your database column name.
## let's set the next one email
$item['email'] = (string)$row->email;

$output[] = $item;
return $output;

Now, this is not a very clever thing to do, but since we don't want to print anything inside the function and within the class, I guess it is ok to do this. This is like a double jeopardy, because we already spent some memory on the loop over and will be spending the same amount for the foreach loop below

## instantiate the clas here
## call your function $something = $Customer -> getCustomer($_GET[customer_id]);

now the part that I don't like

foreach($something as $info){
echo $info['name']."<br/>";
echo $info['email']."<br/>";


ok, after giving this question much thoughts over a gallon of starbucks coffee, I decided to write and test based on the environment similar to the question.

This script uses a class, and then utilizing the PDO FETCH_CLASS. So, instead of investing some memory twice for the return of one, we will utilize the FETCH_CLASS by PDO, which is deemed to be the only way of doing things properly.

Feel free to modify this script for your needs.. I tested it under the php 5.X environment and it is working the way I expected it to be.

Here you have it.. the script is well commented. Please ignore the Morpheus an Neo thing, I just got carried away.. :)

## once again this script is written by PoorBoy or veedeoo.
## for whatever it's worth, feel free to use it.

class video{
## warning this $title refers to the database table column "title"
public $title;
## same as above
public $remotethumb;
## same as the two variables above
public $tags;
## assign description
public $description;
## This will be the output, either  print or echo which I always prefered
## of course we can do anything with the variables as declared above.
public function echo_something(){
## make the title red
$red ="";
$red .='<b style="color:red;">'.$this->title.'</b><br/>';
$red .='<b style="color:navy;">'.$this->description.'</b><br/>';
## if we want to add more values from the query, we can always do so by extending $red variables.
## in fact, we can even use this class to ouput a thumbnail base on the url of the thumbnails from the media table..
$red .= '<img src="'.$this->remotethumb.'"/><br/>';
 return $red;

## settings.inc is a file where the database user, password, and db name is located.
## they are defined as follows; $db_host = "localhost";$db_user = "username"; $db_password = "db password";$db_database = "dbName";
include_once 'includes/settings.inc';

try {
    $someDb = new PDO("mysql:host=$db_host;dbname=$db_database", $db_user, $db_password);
   ## double check and make sure we are connected. If we are, tell Morpheous and Neo about it, well yah.
   ## remove this in production site. we don't want the agents to know we are connected with Morpheus and Neo :).
    echo 'Yes, we are connected Morpheous and Neo!<br />';

   ## define our connection query
   ## don't forget to change media to your database table.
   $query = "SELECT * FROM media WHERE category='Pink Floyd' ";

    ## prepare to fetch PDOStatement objects 
    $pdoObject = $someDb->query($query);

    ## Now, fetch that class video above. using the FETCH_CLASS pdo object.
    $obj = $pdoObject->fetchALL(PDO::FETCH_CLASS, 'video');

  ## Unlike my first not so good example this will only take place once, and not twice.
    foreach($obj as $video)
     ## remember the echo_something in our class video above? This is where we can call it.
        echo $video->echo_something().'<br />';
    ## I think it will be worth it if we close whatever we opened to complete our operation. Codes below can do just that.
    $someDb = null;
## We can either eliminate these remaining two lines below if needed be, but it is highly necessary when testing your class or the script.
catch(PDOException $e)
    echo $e->getMessage();

Remember though.. this

try {

## some stuffs here


is not mandatory, but it is nice to be there. It will truly help people to write better codes. Especially for the templating environment like smarty, dwoo and others.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.