tags:

views:

55

answers:

3

my query throwing 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

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 its throwing Null row,

Can u please re consider this query make change with out error ,

thanks

+2  A: 

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.

John Conde
hi thanks for pointing..http://pastebin.com/m4fbf313dPlease look ,,,
Bharanikumar
A: 

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.

Bill Karwin
the column count is same know , what i have to changes here
Bharanikumar
Just use `$row->cHospital` for all rows.
Bill Karwin
Query is perfect now, u see i got , 1st row as null , how to eliminate that..
Bharanikumar
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.
Bill Karwin
A: 

MY problem fixed...

Thanks to team...

Bharanikumar
It is the custom on StackOverflow to give an upvote for answers that helped you, and to mark as the accepted answer the answer that helped you the most.
Bill Karwin