Solved

pdo fetch class

Posted on 2013-12-05
2
282 Views
Last Modified: 2013-12-06
http://net.tutsplus.com/tutorials/php/why-you-should-be-using-phps-pdo-for-database-access/

class secret_person{
  public $name;
  public $addr;
  public $city;
  public $other_data;
  
  function __construct($other=''){
    $this->address=preg_replace('/[a-z]/','x',$this->address);
    $this->other_data=$other;
  }
}

# connect to the database
try{
  $DBH = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);  
  $DBH->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
  #using the shortcut->query() method here since there is no variable
  #values in the select statement.
  $STH=$DBH->query('SELECT name,addr,city from test');
  #setting the fetch mode
  $STH->setFetchMode(PDO::FETCH_CLASS,'secret_person');
  echo '<br>';
  while($obj=$STH->fetch()){
    //echo $obj->name.'<br>';
    echo $obj->addr.'<br>';
    //echo $obj->city.'<br>';
  }
  
}
catch(PDOException $e){
  echo '<br>no results';
  echo '<br>$e:'.$e;
  echo '<br>$e->getMessage():'.$e->getMessage();
  file_put_contents('PDOErrors.txt',$e->getMessage(),FILE_APPEND);

Open in new window


The addr is not being replaced with x
But when I do use an ide I see that $obj->addr is accessing the constructor of the secret_person class and stepping through the replace statement.
0
Comment
Question by:rgb192
2 Comments
 
LVL 31

Accepted Solution

by:
Marco Gasi earned 500 total points
ID: 39700281
Be careful: you declare public $addr but use $this->address...
0
 

Author Closing Comment

by:rgb192
ID: 39700357
I changed $this->address
to
$this->addr

thanks
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question