Considering the table email_templates:
CREATE TABLE IF NOT EXISTS `email_templates` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` tinytext NOT NULL DEFAULT '',
`display_name` tinytext NOT NULL DEFAULT '',
`from` text NOT NULL DEFAULT '',
`to` text NOT NULL DEFAULT '',
`subject` tinytext NOT NULL DEFAULT '',
`body` mediumtext NOT NULL DEFAULT '',
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=latin1;
id name display_name from to subject body
1 maintenance_request_customer lorem ipsum
The following code:
$sql = "SELECT ? FROM `email_templates` WHERE `name` = ?";
$stmt = mysqli_prepare($link, $sql);
$params = array('body', 'maintenance_request_customer');
mysqli_stmt_bind_param($stmt, "ss", ...$params);
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt, $body);
mysqli_stmt_fetch($stmt);
error_log($body);
Outputs:
'body'
And the following code:
$sql = "SELECT ? FROM `email_templates` WHERE `name` = ?";
$stmt = mysqli_prepare($link, $sql);
$params = array('body', 'maintenance_request_customer');
mysqli_stmt_bind_param($stmt, "ss", ...$params);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
while ($row = $result->fetch_assoc()) {
error_log(var_export($row, true));
}
Outputs:
array (
'?' => 'body',
)
In both cases I'm expecting to get the value lorem ipsum
in some fashion.
In either case I'm not getting the value. I'd prefer to be using the method that uses mysqli_stmt_get_result
however I'm wondering why neither method is working as expected. Much appreciated!