14

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?

5
  • You did not enter in the while () block, so none of the variables are set when you try to return them. Commented Mar 17, 2014 at 21:21
  • No rows are fetched, the loop never start, you might want to return only if rows are found Commented Mar 17, 2014 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. Commented Mar 17, 2014 at 21:22
  • I would guess your query isn't running as expected and you are getting to the return line with undefined variables. Commented Mar 17, 2014 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. Commented Mar 17, 2014 at 21:27

6 Answers 6

27

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;
Sign up to request clarification or add additional context in comments.

2 Comments

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.
never mind I already solved the problem the problem lay with the query where I had declared.
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.

Comments

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.

1 Comment

Yes, this is closer to a real answer.
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) ;

Comments

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.

3 Comments

That could be a comment
@Fabio I answered the question...?
Not returning the array will eschew the notices here, but likely just shift the problem one scope up.
0

XAMPP. I guess you're using MySQL.

mysql_fetch_array($result);

And make sure $result is not empty.

2 Comments

He is pretty clearly using Postgres.
pg_fetch_array() used in the example code is under "PHP Manual""Function Reference""Database Extensions""Vendor Specific Database Extensions""PostgreSQL""PostgreSQL Functions" .

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.