In my application, I have a general query that applies to multiple users. There are cases where the table structure may vary between users. I have a query that I want to apply only to users where a column exists in its table.
function get_item($user_id) {
global $dbh;
$sth = $dbh->query ("SELECT item_type FROM items WHERE user_id = '$user_id'");
$row = $sth->fetch();
$item_type = $row['item_type'];
return $item_type;
}
If the column "item_type" does not exist in my table, I want to ignore it and set the $ item_type variable to NULL.
For these users, I get an error in the code request line:
Fatal error: thrown a 'PDOException' exception with the message 'SQLSTATE [42S22]: Column not found: 1054 Unknown column' item_type 'in the' list of fields' in / item _display.php: 5
Any ideas?