dontmindmeyo dontmindmeyo - 2 months ago 9
PHP Question

Selecting column dynamically with curly braces

I am trying to select a column with curly braces, but the column has a prefix, for example:

//sql to run
$sql = "SELECT * FROM trades
WHERE trade_market = ?
AND trade_buysell = ?
" . $order . "";

//run the sql
$tickersHigh = $database->prepare($sql);
$tickersHigh->execute(array(strtolower($market), $buysell));

//fetch the results
$tickerHigh = $tickersHigh->fetch();

//return the results
return $tickerHigh->trade_{$type};


Since I have added trade_ to my column name it no longer works, it used to work like so:

return $tickerHigh->{$type};


But I recently added the prefix
trade_
to my columns, like so:

return $tickerHigh->trade_{$type};


And I get the error:


Column not found: 1054 Unknown column 'PRICE' in 'order clause'


It should be selecting
trade_price
rather than just
price


so how can I get this to work with my prefix too?

My table:

CREATE TABLE `trades` (
`trade_id` int(11) NOT NULL,
`trade_amount` text NOT NULL,
`trade_market` varchar(25) NOT NULL,
`trade_cost` varchar(25) NOT NULL,
`trade_fee` varchar(25) NOT NULL,
`trade_time` time NOT NULL,
`trade_user` varchar(25) NOT NULL,
`trade_ip` varchar(25) NOT NULL,
`trade_price` text NOT NULL,
`trade_buysell` text NOT NULL,
`trade_date` datetime NOT NULL,
`trade_maincoin` text NOT NULL,
`trade_charttime` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Answer

try

return $tickerHigh->{"trade_$type"};
Comments