Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

pdo fetch class

Posted on 2013-12-05
2
Medium Priority
?
312 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 2000 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

773 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