I have an sql table looking like this
--------------------------------- | id | di | field | content | --------------------------------- | 1 | kh | dvh | lorem ips | --------------------------------- | 2 | kh | kkf | hor ameti | --------------------------------- | 3 | db | hgd | usyytutt | ---------------------------------
What I would like is a way to select from sql and return the variables where field
becomes the column name; like so:
$sql = $conn->prepare('SELECT * FROM table WHERE `di` = "kh"'); // And some more fancy code $sql = execute(); $row = $sql->fetch(PDO::FETCH_ASSOC); echo $row['dvh']; // outputs 'lorem ips'
Currently I am using php to convert the returned array by using:
$result = $sql->fetchAll(PDO::FETCH_ASSOC);
and then running them through a foreach loop:
$data = array(); foreach ($result as $k => $val) { $data[$val['field']] = $data['content']; }
I really want SQL to do this for me.