I was wondering if there is a way to do the following which does not throw me an error. Would someone be able to explain why the following is not possible, and how I can pull it off?
Here is the function:
public function getTaxClass()
{
$arg = func_get_args();
$pid = array_shift($arg);
$imp = implode(',',$arg);
$stmt = _DB::init()->prepare("SELECT $imp
FROM tax_class a
INNER JOIN products_to_tax_class pa ON a.tid = pa.tid
WHERE pa.pid = ?"
);
if($stmt->execute(array($pid)))
return $stmt->fetch(PDO::FETCH_ASSOC);
}
Am I unable to insert variable into a prepared statement? I tried building the string before adding it into the prepare however I still get the following warning:
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'tid' in field list is ambiguous' in /class.php:89 Stack trace: #0 /class.php(89): PDOStatement->execute() #1 /class.php(98): _Class->getTaxClass(1, 'tid', 'name', 'rate') #2 {main} thrown in/class.php on line 89
The error states that the column
tidis ambiguous, because it is included in both you aliasedatable andbtable. To clear that up, it needs to appear in theSELECTcolumns list as eithera.tidORb.tid. By yourimplode()it’s only arriving into the select list astid.The simplest way to fix it would be to specify the table and column as
table.columnto all of your columns when you pass them in as the function arguments.Or, you could prepend the table like so, before doing the
implode():