Search code examples
phpclass-method

php classes and objects to retrieve data from database


I have some doubts in php classes and objects. Just assume that, database contain member details like member id name,age,dob,address etc, i need to retrive member details by passing member id by php classes and object. Sorry for my english,

<?php
  class members{
  public $var;
  public function displaymembername($var){

  require 'config.php';
  $reg= $this->var;
   $sql = "SELECT  student_name, class_name, reg_no FROM students_profile WHERE student_id='$reg'";
    $result = $conn->query($sql);

  if ($result->num_rows > 0) {

   while($row = $result->fetch_assoc()) {
      echo "Reg no : " . $row["reg_no"]. " - Name: " . $row["student_name"]. " Class " . $row["class_name"]. "<br>";
    }
  }
      return $var;       
    }
}
   $displayname = new members;

  echo $displayname->displaymembername(24);
 ?>

Thanks in advance


Solution

  • You can start with changing $reg= $this->var; to `$req=$var´ or assign ´$var´ to ´$this->var´ before using it and see if that works – rypskar Oct 12 '16 at 11:41

    This solves my problem