php - codeigniter 将字段名称和行值放入单个字符串中

标签 php mysql sql codeigniter

我想将字段名称和行值放入一个字符串中。

例子:
编号 |姓名 |性别 |电子邮件
1 |约翰 |男性 | 1@1.com
2 |玛丽 |女性 | 2@2.com

select * from table where id=1  
$this->db->where('id', 1);
$query = $this->db->get('table');
$row = $query->row();
$string = $row->id . $row->fname . $row->gender . $row->email;

期望结果:
id:1, fname:john, gender:male, email:1@1.com
或者
id, fname, gender, email, 1, john, male, 1@1.com

任何关于如何动态构建 sql 查询的建议,非常感谢。

最佳答案

$query = $this->db->query("select whatever from wherever.");
$string = "";
foreach ($query->result_array() as $row)
{
    foreach($row as $key=>$val){$string.="$key:$val,";}
}

关于php - codeigniter 将字段名称和行值放入单个字符串中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23209262/

相关文章:

php - 带评论的新闻数据库,如何使用 mysql 和 PHP 消除重复项?

php - 使用 php 保存 Jquery 可拖动 DIV 的位置

mysql - 如何通过MYSQL语句选择这些记录?

sql - Postgresql - 创建用户的累计总和

c# - 如何使用类似 C# 的行为在 SQL 中进行相等比较?

php - 正则表达式用逗号和空格分割字符串,但忽略内部引号和括号

javascript - Braintree 的 Sandbox PayPal 集成(使用 JS 和 PHP)

java - 标准 API : Order/Select by implicit related table

mysql - 连接与子查询来计算嵌套对象的数量

mysql语法错误为什么?