0

I am working on a PHP script that should get data from MySQL.

Here is what am I doing:

<?php
include('db.php');
session_start();
$doctor_actual=$_SESSION['doctor_actual'];
echo $doctor_actual;
 if(isset($_REQUEST['actionfunction']) && $_REQUEST['actionfunction']!=''){
$actionfunction = $_REQUEST['actionfunction'];

   call_user_func($actionfunction,$_REQUEST,$con,$limit,$adjacent);
}
function showData($data,$con,$limit,$adjacent){
  $page = $data['page'];
   if($page==1){
   $start = 0;  
  }
  else{
  $start = ($page-1)*$limit;
  }
  $sql = "select * from tb_opiniones_doctor where codigo_verificacion = '".$doctor_actual."' order by id_opinion_doctor asc";
  $rows  = $con->query($sql);
  $rows  = $rows->num_rows;

  $sql = "select * from tb_opiniones_doctor where codigo_verificacion = '".$doctor_actual."'  order by id_opinion_doctor asc limit $start,$limit";

  $data = $con->query($sql);
  $str='<table><tr class="head"><td>Id</td><td>Firstname</td><td>Lastname</td></tr>';
  if($data->num_rows>0){
   while( $row = $data->fetch_array(MYSQLI_ASSOC)){
      $str.="<tr><td>".$row['id_opinion_doctor']."</td><td>".$row['id_opinion_doctor']."</td><td>".$row['id_opinion_doctor']."</td></tr>";
   }
   }else{
    $str .= "<td colspan='5'>No Data Available</td>";
   }
   $str.='</table>';

echo $str; 
pagination($limit,$adjacent,$rows,$page);  
}

My problems are at the two queries, they only work if I put the real value for $doctor_actual, not as variable.

I have echoed the value for $doctor_actual, it is 9dv2ACvtwn2.

If I put in the queries ..where codigo_verificacion = "9dv2ACvtwn2"... the queries work fine.

If I put:

codigo_verificacion = '".$doctor_actual."'

or

codigo_verificacion = '.$doctor_actual.'

or

codigo_verificacion = $doctor_actual

it shows the message:

No Data Available

1 Answer 1

3

You should read about Variable scope. $doctor_actual outside function and $doctor_actual inside function are two different variables. As you can read above something like that

<?php
$var = 'text';

function myFunc()
{
   global $var;
   echo $var; // 'text'
}

will solve your problem.


But as noticed @Sean in comments below it's better idea to pass value as a parameter. Just add additional parameter to your function and pass value during function call.

Sign up to request clarification or add additional context in comments.

3 Comments

Although technically correct, it is typically not encouraged to recommend using global -> global $var;. As they are already using function parameters, why not recommend that that add it there.
You're right, pass value as a parameter is better idea, I'll update my answer, thanks!
Thank you both, I have added $doctor_actual as parameter to the function and works fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.