-2

I was trying to pullout the content of all the functions defined from the class segment in segment.php. And calling all the three functions from a class segment in home_segment.php Is this a correct way of calling multiple functions from one class.

segment.php

<?php
$mysqli = mysqli_connect("localhost","root","","square");
 $cid = required_param('id', PARAM_INT);
 class segment
 {   
   public function unitseg_set1($cid)
   {          
     $subject = $mysqli->query('SELECT * FROM order_subject WHERE id='.$cid.'');     
     while($row=$subject->fetch_array() )
     {
        echo '<div>'.$row['chem_name'].'</div>';
     }   
   }

   public function unitseg_set2($cid)
   {          
     $subject = $mysqli->query('SELECT * FROM order_subject WHERE id='.$cid.'');     
     while($row=$subject->fetch_array() )
     {
        echo '<div>'.$row['physiotherapy_nn'].'</div>';
     }   
   }   

   public function unitseg_set3($cid)
   {          
     $subject = $mysqli->query('SELECT * FROM order_subject WHERE id='.$cid.'');     
     while($row=$subject->fetch_array() )
     {
        echo '<div>'.$row['commun_gg'].'</div>';
     }   
   }   
 }
 ?>

home_segment.php

<?php


    require_once('segment.php');
   $account1 = segment::unitseg_set1($cid);
   $account2 = segment::unitseg_set2($cid);
   $account3 = segment::unitseg_set3($cid);

   echo $account1.$account2.$account3;
 ?>

2 Answers2

2

Those methods are not defined as static so you can't use the :: syntax (http://php.net/manual/en/language.oop5.paamayim-nekudotayim.php). You have to first instantiate the class as an object and then you can call those methods using the -> operator. You will have to have access to your $cid variable so that you can pass it in.

I have to mention that your SQL queries are wide open to SQL injection attacks. Please look up PDO and parameterized queries. https://phpdelusions.net/pdo.

<?php

require_once('segment.php');
$cid = required_param('id', PARAM_INT);
$segment = new segment();
$account1 = $segment->unitseg_set1($cid);
$account2 = $segment->unitseg_set2($cid);
$account3 = $segment->unitseg_set3($cid);

echo $account1.$account2.$account3;
waterloomatt
  • 3,662
  • 1
  • 19
  • 25
  • I tried to display the same, it shows "error reading from database". – user1067018 Jun 26 '18 at 13:26
  • Please turn on error reporting in your scripts to see the actual error. Follow this guide - https://stackoverflow.com/q/22662488/296555 – waterloomatt Jun 26 '18 at 13:34
  • It shows Undefined variable: account1, account2, account3 – user1067018 Jun 26 '18 at 13:58
  • 1 issue I see is that your class methods use `echo` directly which you're then trying to assign to a variable. They should probably be `return` instead, after which the _calling code_ will `echo` it. Unfortunately, we're not going to be able to help you through these comment boxes. Please ask a new question and post all the relevant code including which lines are causing the issue. – waterloomatt Jun 26 '18 at 14:31
1

The :: is for methods defined as static. Either define you methods as static, as in public static function ..., or instantiate the class, as in:

require_once('segment.php');
$segment = new segment();
$account1 = $segment->unitseg_set1($cid);
$account2 = $segment->unitseg_set2($cid);
$account3 = $segment->unitseg_set3($cid);

echo $account1.$account2.$account3;

Also, as waterloomatt pointed out, you are wide open to SQL injection. You need to use prepared statements, rather than concatenating variables into your query. See How can I prevent SQL injection in PHP?.

elixenide
  • 44,308
  • 16
  • 74
  • 100