3

My query is throwing an error like

select id,cHospital from med_patient where cHospital is not null  union
select id,cHospital1 from med_patient where cHospital1 is not null  union
select id,cHospital2 from med_patient where cHospital2 is not null  order by 1

The error is

Notice: Undefined index: cHospital1 in F:\wamp\www\mmr-final\admin\all_hospital_list.php on line 97

Notice: Undefined index: cHospital2 in F:\wamp\www\mmr-final\admin\all_hospital_list.php on line 98

Notice: Undefined index: cHospital1 in F:\wamp\www\mmr-final\admin\all_hospital_list.php on line 97

Notice: Undefined index: cHospital2 in F:\wamp\www\mmr-final\admin\all_hospital_list.php on line 98

Notice: Undefined index: cHospital1 in F:\wamp\www\mmr-final\admin\all_hospital_list.php on line 97

Notice: Undefined index: cHospital2 in F:\wamp\www\mmr-final\admin\all_hospital_list.php on line 98

Notice: Undefined index: cHospital1 in F:\wamp\www\mmr-final\admin\all_hospital_list.php on line 97

Notice: Undefined index: cHospital2 in F:\wamp\www\mmr-final\admin\all_hospital_list.php on line 98

Notice: Undefined index: cHospital1 in F:\wamp\www\mmr-final\admin\all_hospital_list.php on line 97

Notice: Undefined index: cHospital2 in F:\wamp\www\mmr-final\admin\all_hospital_list.php on line 98

Notice: Undefined index: cHospital1 in F:\wamp\www\mmr-final\admin\all_hospital_list.php on line 97

Notice: Undefined index: cHospital2 in F:\wamp\www\mmr-final\admin\all_hospital_list.php on line 98`

And also it's throwing Null row.

How can this query be changed to not cause an error?

3
  • The problem isn't the query, but the PHP. Can you post the full file or at least more of the surrounding code? Commented Jan 15, 2010 at 19:08
  • pastebin.com/m4fbf313d Commented Jan 15, 2010 at 19:23
  • i tell u what i am trying exactly.. see i have three fields , chospital,chospital1,chospital2, in tbl1 table,i have around 50 patient entries, some patient may have chostial1,chosptail2, some patient have one chopsital code, i want render all code from the tbl2 table, i dont want duplication in my outout, also no null values.. Commented Jan 15, 2010 at 19:26

2 Answers 2

2

Those are not MySQL errors. They are PHP errors. Basically you are most likely attempting to use variables without declaring them first. Be sure to always declare your variables before using them. It makes you less like to have errors.

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

Comments

0

In a UNION query, the column names must be the same for all rows. So it uses the column names from the first query in the union. Column names in subsequent queries in the union are ignored.

In other words, in this query:

select id,cHospital from med_patient where cHospital is not null union 
select id,cHospital1 from med_patient where cHospital1 is not null union 
select id,cHospital2 from med_patient where cHospital2 is not null order by 1

The column names on all rows are: id and cHospital.

So in your PHP code, when you fetch the result as an object and try to reference a field of the object like this:

$cHospital1 = $row->cHospital1;
$cHospital2 = $row->cHospital2;

The object doesn't have fields by those names. It only has $row->cHospital, even for rows that came from the second and third queries in the union.

3 Comments

the column count is same know , what i have to changes here
Just use $row->cHospital for all rows.
NULL is not the same as an empty string '' in SQL. If you want to restrict against rows where the hospital is blank, put that condition in the WHERE clauses.

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.