gpt4 book ai didi

PHP MySQL : Select column with title like year

转载 作者:行者123 更新时间:2023-11-30 00:25:22 25 4
gpt4 key购买 nike

我的表格('contas')包含与年份(2014、2015 等...)相对应的列以及其他列,例如:

id   | name   | notes   |   2014   | 2015   | 2016
1 | abc | whatever| 5.25 | 8.50 | 9.50

我想使用 PHP 从该表中选择与当前年份相对应的列。

我有:

$ano=date('Y');
$ano=(string)$ano;

$sql = "SELECT `id`, `name`, `notes`, $ano AS 'saldo' FROM `contas` WHERE ...";

$result = $pdo->query($sql);
$rows = $result->fetchAll(PDO::FETCH_ASSOC);

在“saldo”列中,我为每一行获取 2014,而不是表的值。

我尝试将 $ano 插入查询中的单引号和双引号中,但没有成功。

最佳答案

转义列名称..

$sql = "SELECT `id`, `name`, `notes`, `$ano` AS 'saldo' FROM `contas` WHERE ...";

关于PHP MySQL : Select column with title like year,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22933941/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com