php - 用数据库值替换字符串中的字符
问题描述
我有字符串消息$message = 'Dear $firstname$ $lastname$, your point of city is $city$'
我想从 Mysql 数据表中获取值$firstname$, $lastname$
并$city$
替换为消息。
我从数据库中选择了多个用户数据,并将新消息存储到数组中。
$query = $ab->pquery("select firstname, lastname, city from tblaccount where country = $country";
$numOfrows = $db->num_rows($query);
for($i=0; $i<$numOfrows; $i++) {
$toNumber = array();
$toNumber['firstname'] = $db->query_result($result, $i, 'firstname');
$toNumber['lastname'] = $db->query_result($result, $i, 'lastname');
$toNumbers[] = $toNumber;
}
我希望为所有用户动态替换名字、姓氏和城市,并将消息存储到数组中。
如何识别$firstname$
替换为数据库归档的结果firstname
?
解决方案
另一种方法是让 MySQL 完成连接工作,这将减少 PHP 指令。
$messageSQL = "CONCAT('Dear ', firstname, ' ', lastname, ', your point of city is ', city)";
$query = $ab->pquery("select $messageSQL AS themessage from tblaccount where country = $country";
$numOfrows = $db->num_rows($query);
for($i=0; $i<$numOfrows; $i++) {
$message = $db->query_result($result, $i, 'themessage');
$toNumbers[] = $message;
}
推荐阅读
- flutter - 可视化用于颤振测试(或颤振驱动程序)的敲击/手势
- python - 嗨,我有一个 python 脚本可以使用 openpyxl 处理 excel 数据,但是在 2500 行之后我得到了内存错误。谁可以帮我这个事?
- ios - 带有 Epson ePOS SDK 的 Flutter 插件 - 未构建
- reactjs - 如何在反应中解决这个未处理的拒绝错误?
- json - 从 Bash 中的字符串中提取数据
- python - 以junit xml格式保存测试用例
- android - 如何用通用响应包装改造响应
- ansible - 在ansible中读取具有未知键的vars文件
- html - Django 模态表单需要一个值
- docker - 使用 AWS CDK 将图像上传到 ECR 时可以绕过 docker 登录吗?