12

Code:

Function ShowDataPatient($idURL)
{
    $query =" select * from cmu_list_insurance,cmu_home,cmu_patient where cmu_home.home_id = (select home_id from cmu_patient where patient_hn like '%$idURL%')
                     AND cmu_patient.patient_hn like '%$idURL%'
                     AND cmu_list_insurance.patient_id like (select patient_id from cmu_patient where patient_hn like '%$idURL%') ";

    $result = pg_query($query) or die('Query failed: ' . pg_last_error());

    while ($row = pg_fetch_array($result))
    {
        $hn = $row["patient_hn"];
        $pid = $row["patient_id"];
        $datereg = $row["patient_date_register"];
        $prefix = $row["patient_prefix"];
        $fname = $row["patient_fname"];
        $lname = $row["patient_lname"];
        $age = $row["patient_age"];
        $sex = $row["patient_sex"];
    }
    return array($hn, $pid, $datereg, $prefix, $fname, $lname, $age, $sex);
}

Error:

Notice: Undefined variable: hn in C:\xampp\htdocs\...
Notice: Undefined variable: pid in C:\xampp\htdocs\...
Notice: Undefined variable: datereg in C:\xampp\htdocs\...
Notice: Undefined variable: prefix in C:\xampp\htdocs\...
Notice: Undefined variable: fname in C:\xampp\htdocs\...
Notice: Undefined variable: lname in C:\xampp\htdocs\...
Notice: Undefined variable: age in C:\xampp\htdocs\...
Notice: Undefined variable: sex in C:\xampp\htdocs\...

How can I fix that?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Beebrabie
  • 141
  • 1
  • 1
  • 6
  • You did not enter in the `while ()` block, so none of the variables are set when you try to return them. – fedorqui Mar 17 '14 at 21:21
  • No rows are fetched, the loop never start, you might want to return only if rows are found – Fabio Mar 17 '14 at 21:21
  • If your query returns nothing, there'll be no `$row` and subsequently your local variable copies be undefined. So, work on the query. – mario Mar 17 '14 at 21:22
  • I would guess your query isn't running as expected and you are getting to the return line with undefined variables. – Mike Brant Mar 17 '14 at 21:22
  • It appears that you only expect at most one row to be returned, so you could simply `return pg_fetch_array($result)` and do away with all of the variables. – Dr. McKay Mar 17 '14 at 21:27

6 Answers6

26

Define the variables at the beginning of the function so if there are no records, the variables exist and you won't get the error. Check for null values in the returned array.

$hn = null;
$pid = null;
$datereg = null;
$prefix = null;
$fname = null;
$lname = null;
$age = null;
$sex = null;
simhumileco
  • 31,877
  • 16
  • 137
  • 115
mseifert
  • 5,390
  • 9
  • 38
  • 100
  • After that is done there is another query what selects data of a form that has been filled prior to this and that data has to be displayed under the right department title because there are people with more than one department to their name. – BRoebie Nov 23 '15 at 08:52
  • never mind I already solved the problem the problem lay with the query where I had declared. – BRoebie Nov 23 '15 at 10:39
10

Declare them before the while loop.

$hn = "";
$pid = "";
$datereg = "";
$prefix = "";
$fname = "";
$lname = "";
$age = "";
$sex = "";

You are getting the notice because the variables are declared and assigned inside the loop.

Fabio
  • 23,183
  • 12
  • 55
  • 64
akr
  • 739
  • 4
  • 15
4

I would guess your query isn't running as expected and you are getting to the return line with undefined variables.

Also, the way you are doing the variable assignment, you would be overwriting the same variable with each loop iteration, so you wouldn't return the entire result set.

Finally, it seems odd to return a numerically-keyed result set instead of an associatively-keyed one. Consider naming only the fields needed in the SELECT and keeping the key assignments. So something like this:

Function ShowDataPatient($idURL){
       $query =" select * from cmu_list_insurance,cmu_home,cmu_patient where cmu_home.home_id = (select home_id from cmu_patient where patient_hn like '%$idURL%')
                 AND cmu_patient.patient_hn like '%$idURL%'
                 AND cmu_list_insurance.patient_id like (select patient_id from cmu_patient where patient_hn like '%$idURL%') ";

   $result = pg_query($query) or die('Query failed: ' . pg_last_error());

   $return = array();
   while ($row = pg_fetch_array($result)){
       $return[] = $row;
   }          

   return $return;
}

You might also consider opening a question about how to improve your query, is it is pretty heinous as it stands now.

Mike Brant
  • 70,514
  • 10
  • 99
  • 103
4

You should initialize your variables outside the while loop. Outside the while loop, they currently have no scope. You are just relying on the good graces of php to let the values carry over outside the loop

           $hn = "";
           $pid = "";
           $datereg = "";
           $prefix = "";
           $fname = "";
           $lname = "";
           $age = "";
           $sex = "";
           while (...){}

alternatively, it looks like you are just expecting a single row back. so you could just say

$row = pg_fetch_array($result);
if(!row) {
    return array();
}
$hn = $row["patient_hn"];
$pid = $row["patient_id"];
$datereg = $row["patient_date_register"];
$prefix = $row["patient_prefix"];
$fname = $row["patient_fname"];
$lname = $row["patient_lname"];
$age = $row["patient_age"];
$sex = $row["patient_sex"];

return array($hn,$pid,$datereg,$prefix,$fname,$lname,$age,$sex) ;
Zak
  • 24,947
  • 11
  • 38
  • 68
1

It looks like you don't have any records that match your query, so you'd want to return an empty array (or null or something) if the number of rows == 0.

Dr. McKay
  • 2,727
  • 2
  • 15
  • 19
0

XAMPP. I guess you're using MySQL.

mysql_fetch_array($result);

And make sure $result is not empty.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ganu
  • 39
  • 3
  • 2
    He is pretty clearly using Postgres. – Dr. McKay Mar 17 '14 at 21:26
  • [pg_fetch_array()](https://www.php.net/manual/en/function.pg-fetch-array.php) used in the example code is under *"PHP Manual"* → *"Function Reference"* → *"Database Extensions"* → *"Vendor Specific Database Extensions"* → *"PostgreSQL"* → *"PostgreSQL Functions"* . – Peter Mortensen Sep 19 '21 at 21:04