I’m trying to query a single result from two tables in my database. Here is my code:
$pageid = mysql_real_escape_string($_GET['id']);
$query = sprintf("(SELECT * FROM classifieds WHERE pageid = '$pageid' LIMIT 1)
UNION ALL
(SELECT * FROM resumes WHERE pageid = '$pageid' LIMIT 1)");
$result = mysql_query($query);
while ($row = mysql_fetch_array($result)) {
echo $row['title'] . "<br/>";
}
pageid is a URL variable. This code is trying to use that variable, query the database with it, and return the result. I get this error:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource
Hitting a wall here. Can’t figure out why this error keeps happening. Any help == thanks.
When using
UNIONthe number of columns and types of columns, of both queries must be same.Now as you are quering two different tables, I guess it is safe to assume that either number of columns are different or corresponding column types do not match.
try using something like this
but again, number and corresponding column types should be same.