Jump to content

columns as attributes in a class using prepared stmt


chaking

Recommended Posts

Within a class, using a prepared statement with the mysqli query, does anyone know of a way to set the attributes of the class to the column names of the result set?

 

This is how I'm doing it right now, but as you can see it means having to modify it if the contact table changes structure.  I thought I knew how to do it, but apparently throwing the following foreach loop in the middle of the other foreach loop doesn't work...

 

foreach ($parameters as $val) {
$this->$val = $value;
}

 

Any suggestions would be greatly appreciated - Thanks!

 

class contact {
   
   private $email;
   private $first_name;
   private $last_name;
   private $title;
   private $office_phone;
   private $cell_phone;
   private $home_phone;
   private $pager;
   private $other;
   private $date_time;
   private $changed;
   
    function __set($var, $val){
        $this->$var = $val;
    }
   
   function __get($name) {
         if (isset($this->$name)) {
            return $this->$name;
         }
      echo "No such property [$name]<br />";
      return null;
   }

   
   function __construct($email)
   {
      include $_SERVER['DOCUMENT_ROOT']."/ps-security/includes/cxn.php";
      @ $db = new mysqli($host, $user, $passwd, $dbname);
      $query = "SELECT * FROM contacts WHERE email = ?";
      $stmt = $db->prepare($query);
      $stmt->bind_param("s", strip_tags(trim($email)));
      $stmt->execute();
      $stmt->store_result();
      $meta = $stmt->result_metadata(); 
      
         while ($field = $meta->fetch_field()) {
             $var = $field->name;
             $parameters[] = &$$var;
         }
      
      call_user_func_array(array($stmt, 'bind_result'), $parameters); 
         
         if (!$stmt->fetch()) 
         { 
            throw new Exception("<font color=red>No Results Returned in dB</font>"); 
         }
         
      $stmt->close();
         foreach ($parameters as &$value) {
            $value = stripslashes($value);
               if (is_string($value)) {
                  $value = strip_tags($value, '<br><a><strong><u><i><li><p>');
                  $value = html_entity_decode($value, ENT_QUOTES);
               }
            
         }
      
      $this->email = $email;
      $this->first_name = $first_name;
      $this->last_name = $last_name;
      $this->title = $title;
      $this->office_phone = $office_phone;
      $this->cell_phone = $cell_phone;
      $this->home_phone = $home_phone;
      $this->pager = $pager;
      $this->other = $other;
      $this->date_time = $date_time;
      $this->changed = $changed;   
   }

}

 

PS: How do you get your code's syntax highlighted in your posts? Some tag I'm missing?

class contact {
   
   private $_data;
   
    function __set($var, $val){
        $this->_data[$var] = $val;
    }
   
   function __get($name) {
         if (isset($this->_data[$name])) {
            return $this->_data[$name];
         }
      echo "No such property [$name]<br />";
      return null;
   }

   
   function __construct($email)
   {
      include $_SERVER['DOCUMENT_ROOT']."/ps-security/includes/cxn.php";
      @ $db = new mysqli($host, $user, $passwd, $dbname);
      $query = "SELECT * FROM contacts WHERE email = ?";
      $stmt = $db->prepare($query);
      $stmt->bind_param("s", strip_tags(trim($email)));
      $stmt->execute();
      $stmt->store_result();
      $meta = $stmt->result_metadata(); 
      
         while ($field = $meta->fetch_field()) {
             $var = $field->name;
             $parameters[] = &$$var;
         }
      
      call_user_func_array(array($stmt, 'bind_result'), $parameters); 
         
         if (!$stmt->fetch()) 
         { 
            throw new Exception("<font color=red>No Results Returned in dB</font>"); 
         }
         
      $stmt->close();
         foreach ($parameters as &$value) {
            $value = stripslashes($value);
               if (is_string($value)) {
                  $value = strip_tags($value, '<br><a><strong><u><i><li><p>');
                  $value = html_entity_decode($value, ENT_QUOTES);
               }
               $this->_data[$parameters] = $value;
            
         }  
   }

}

Thanks Thorpe!

 

I keep getting undefined offsets however... Result:

 

Warning: Illegal offset type in C:\Apache2.2\htdocs\ps-security\admin\class_contact.php on line 50

....

 

Thanks again for taking time to look at it -

 

I'm running 5.2.6 -- It wouldn't be something added in 5.3 would it?

Ok... got it, but not completely.. With this you can use numerics to access columns, but not string references... Still, better than before, just now it would need a key to go with it telling people what number represents what...

 

<?php
class contact {
   
   private $data;
   
    function __set($var, $val){
        $this->data[$var] = $val;
    }
   
   function __get($name) {
         if (isset($this->$name)) {
            return $this->$name;
         }
      echo "No such property [$name]<br />";
      return null;
   }

function show() {

echo $this->data[0];
echo $this->data[1];

}

   
   function __construct($email)
   {
      include $_SERVER['DOCUMENT_ROOT']."/ps-security/includes/cxn.php";
      @ $db = new mysqli($host, $user, $passwd, $dbname);
      $query = "SELECT * FROM contacts WHERE email = ?";
      $stmt = $db->prepare($query);
      $stmt->bind_param("s", strip_tags(trim($email)));
      $stmt->execute();
      $stmt->store_result();
      $meta = $stmt->result_metadata(); 
      
         while ($field = $meta->fetch_field()) {
             $var = $field->name;
             $parameters[] = &$$var;
         }
      
      call_user_func_array(array($stmt, 'bind_result'), $parameters); 
         
         if (!$stmt->fetch()) 
         { 
            throw new Exception("<font color=red>No Results Returned in dB</font>"); 
         }
         
      $stmt->close();
         foreach ($parameters as $key => &$value) {
            $value = stripslashes($value);
               if (is_string($value)) {
                  $value = strip_tags($value, '<br><a><strong><u><i><li><p>');
                  $value = html_entity_decode($value, ENT_QUOTES);
               }
               $this->data[$key] = $value;
            
         }  
   }

}

?>

 

The deal was in the foreach statement... $parameters wasn't actually the column name, rather an array... so separating it with the $key => $value clause turned key into the numerical representation of the place in the array and thus was able to be used successfully.

 

Good stuff, still working on getting column names referenced, but this works for now... thanks!

 

So here's how it would need to be called:

<?php

$vari = new contact($email);
echo $vari->data[0]; //echos output from the first column... column 2 would be data[1] and so on...

//or from the show method

$vari->show();

?>

Ah Ha!

 

Create an array while creating the parameters array as so (data2):

<?php
while ($field = $meta->fetch_field()) {
         $var = $field->name;
         $parameters[] = &$$var;
         $data2[] = $var;
}
?>

 

Then use that array of column names (gathered from $meta->fetch_field) to populate, while using the numeric reference from $parameters ($key) to keep place...

 

<?php
foreach ($parameters as $key => &$value) {
$value = stripslashes($value);
               if (is_string($value)) {
                   $value = strip_tags($value, '<br><a><strong><u><i><li><p>');
                   $value = html_entity_decode($value, ENT_QUOTES);
                }

       $value2 = $data2[$key];
       $this->data[$value2] = $value;
            
} 
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.