I just realized my mistake! I used the table jokecategory twice… learning PHP is a humbling experience…
Thank you for your help, so far so good!
Sorry I didn’t paste in the SQL, but your advice helped nonetheless. I modified the SQL query to use “AS”, however I think I mixed something up with the “category” table. Can’t figure out what, but it’s not allowing me to search based on category now. Any ideas? I am receiving the following error:
Error: Not unique table/alias: ‘jokecategory’
SQL query:
$select = 'SELECT DISTINCT joke.id, joke.joketext, author.id AS author_name, author.name AS author_name, jokecategory.jokeid AS cat_jokeid, jokecategory.categoryid AS joke_catid, category.id AS cat_id, category.name as cat_name';
$from = ' FROM joke, author, jokecategory, category';
$where = ' WHERE joke.authorid = author.id AND joke.id = jokecategory.jokeid AND jokecategory.categoryid = category.id';
$aid = $_POST['aid'];
if ($aid != '') { // An author is selected
$where .= " AND authorid='$aid'";
}
$cid = $_POST['cid'];
if ($cid != '') { // A category is selected
$from .= ', jokecategory';
$where .= " AND joke.id=jokecategory.jokeid AND categoryid='$cid'";
}
$tid = $_POST['tid'];
if ($tid != '') { // A theme is selected
$from .= ', joketheme';
$where .= " AND joke.id=joketheme.jokeid AND themeid='$tid'";
}
$gfid = $_POST['gfid'];
if ($gfid != '') { // A region is selected
$from .= ', jokegeofocus';
$where .= " AND joke.id=jokegeofocus.jokeid AND geofocusid='$gfid'";
}
$searchtext = $_POST['searchtext'];
if ($searchtext != '') { // Some search text was specified
$where .= " AND joketext LIKE '%$searchtext%'";
}
?>
We don’t see your SQL query, but you should be supplying column aliases for ambiguous columns with the
ASkeyword, as in:These would then be accessed in a fetched row by their aliases:
A further suggestion (though again since we don’t see the SQL it may be moot), is to be explicit about which columns you select.
Rather than doing
a better practice is to explicitly list the columns you need: