Solved

pdo fetch class

Posted on 2013-12-05
2
286 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
comma true 6 37
PHP PDO   How do I correctly remove record using DELETE FROM? 1 25
PHP Soap Server returning XML but < & > as &lt; / &gt; 9 45
ajax to record click 3 15
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

789 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