How to get mysqli to allow SQL-based User-Defined Variables as column aliases?
| From: | Philip Schlesinger | Date: | Thu, 10 Jun 2021 05:22:11 +0000 |
| Subject: | How to get mysqli to allow SQL-based User-Defined Variables as column aliases? | ||
| Groups: | php.db | ||
| Request: | Send a blank email to php-db+get-49193@lists.php.net to get a copy of this message | ||
Hi all,
I’m able to get mysqli to set a user-defined variable and the use it later on either in a column
value definition or a conditional, I.e. (semi-pseudocode since I’m not in front of my work
computer)
First do mysqli $conn->query(‘Set @foo = “bar”;’);
…which will prepare the database session with the variable.
Then do a $stmt = $conn->stmt_init()
Then do a $stmt->prepare(‘Select @foo as “baz”;’);
Finally do a $stmt->execute();
That results in:
‘baz’
———
“bar”
_however_ if I want to use a SQL user-defined variable as a column alias, mysqli throws an error.
This does not work:
$conn->query(‘set @acolname = “aCol”;’);
$stmt = $conn->stmt_init()
$stmt->prepare(‘Select “data” as @acolname;’);
$stmt->execute();
If I ran the straight SQL in a SQL application, making the select statement a variable as well, and
did a prepare and execute, it would work just fine
Mysqli however throws an error with the column alias being a SQL user-defined variable.
Help?
Get Outlook for iOS<https://aka.ms/o0ukef>