$query = "SELECT l.id, l.url, l.thirdparty, lc.category FROM links as l ";
$query .= "LEFT JOIN link_categories AS lc ON lc.id=l.category_fk ";
$query .= "ORDER BY $order $sort ";
$sql = $dbh->prepare($query);
$sql->execute();
$numResults = $sql->rowCount();
$max = 'LIMIT ' .$totaloffset.',' .PERPAGE;
$query .= $max;
$stmt = $dbh->query($query);
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
I guess adding an alias to the first table and adding the prefix to each field name is needed? (apologies if the lingo is off there, any corrections are welcome)
If links and link_categories both had an ‘id’ field then the sql statement would not have worked - but that would clearly be an sql problem and not a PDO problem.
As you built up your query in a variable called $query then you could have echoed $query onto the page somewhere and posted it into PhpMyAdmin which means you would have discovered that sooner.
$query = <<<EOT
SELECT l.id, l.url, l.thirdparty, lc.category FROM links as l
LEFT JOIN link_categories AS lc ON lc.id=l.category_fk
ORDER BY $order, $sort
EOT;
I’m using a paginator class to page results…the first query is to find total number of records, the second is what’s used for outputting the data to the page.
Ah yes…duh. The really simple solutions escape me sometimes.
So even if I’m not asking for the id field of the link_categories table, it fails? I’ve never had that problem when working with the normal mysql functions.