Mysql version 5.1. Code example:
mysql_query("SELECT * FROM abonents WHERE `session_id`='$current_sess_id' UNION SELECT * FROM abonents_problem WHERE `session_id`='$current_sess_id' UNION SELECT * FROM abonents_stop WHERE `session_id`='$current_sess_id'");
Runs if remove "UNION", example:
mysql_query("SELECT * FROM abonents WHERE `session_id`='$current_sess_id'" );
"WTF Code" forever =):
$rez_11_one = mysql_query(
"SELECT * FROM abonents WHERE `session_id`='$current_sess_id'"
);
$rez_11_two = mysql_query(
"SELECT * FROM abonents_problem WHERE `session_id`='$current_sess_id'"
);
$rez_11_three = mysql_query(
"SELECT * FROM abonents_stop WHERE `session_id`='$current_sess_id'"
);
if ($rez_11_one) {
$rez_11 = $rez_11_one;
} elseif ($rez_11_two) {
$rez_11 = $rez_11_two;
} else {
$rez_11 = $rez_11_three;
}
Thank's for help
reference to the mysql documentation UNION
only works if all querys return the same number of columns
.
http://dev.mysql.com/doc/refman/4.1/en/union.html
therefore as you use a selector asterisks *
i think the tables have a different amount of columns. use select
eg
SELECT 1,2 FROM foo UNION SELECT 1,2 FROM bar
to get per query the same amount of columns.
Try query without "*" Like this
mysql_query("SELECT <table_column_name> FROM abonents WHERE `session_id`='$current_sess_id' UNION SELECT <table_column_name> FROM abonents_problem WHERE `session_id`='$current_sess_id' UNION SELECT <table_column_name> FROM abonents_stop WHERE `session_id`='$current_sess_id'");
You may need to add escape characters to the string to make it:
mysql_query("SELECT * FROM abonents WHERE \`session_id\`=\'$current_sess_id\' UNION SELECT * FROM abonents_problem WHERE \`session_id\`=\'$current_sess_id\' UNION SELECT * FROM abonents_stop WHERE \`session_id\`=\'$current_sess_id\'");
Make sure if the query returns the same number of columns..According to the documentation of UNION
.It only works if the query returns same number of columns