热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

PHPmysqli-从准备好的语句返回一个关联数组-PHPmysqli-returnanassociativearrayfromapreparedstatement

Imtryingtousemysqlitoprepareastatementinordertosafelypassinvariablevaluestothequ

I'm trying to use mysqli to prepare a statement in order to safely pass in variable values to the query. All of that is working for me, but the problem I'm running into is getting the result in an associative array. Here's my structure so far:

我尝试使用mysqli准备语句,以便安全地将变量值传递给查询。所有这些都对我有用,但我遇到的问题是在一个关联数组中获得结果。这是我目前的结构:

$query = $c->stmt_init();
$query->prepare("SELECT e._id,e.description,e.eventDate,e.eventTime,e.address,e.locationDescription,i.guestId,r.guestId IS NOT NULL AS 'RSVP-ed'  FROM eventList AS e  JOIN inviteList AS i ON e._id = i.eventId LEFT JOIN rsvpList AS r ON r.eventId = e._id AND i.guestId = r.guestId JOIN guestList AS g ON g._id = i.guestId WHERE g.groupName = ?");
$query->bind_param('s',$groupName);
if ($result = $query->execute()){
    $a  = $result->fetch_array(MYSQLI_ASSOC); // this doesn't work :/
} else{
    error_log ("Didn't work");
}

As you can see, I have a lot of columns getting passed back so I'd like to not have to bind them each to a variable.

正如您所看到的,我有很多列要传回来,所以我不想把它们每个都绑定到一个变量上。

On top of that, the end goal is to pass back a json encoded associative array to the rest of my application.

最重要的是,最终目标是将json编码的关联数组回传给应用程序的其余部分。

I've looked up the issue in the php documentation and on stack exchange and I've found suggestions, but I can't seem to get them to work. Could anyone lend a hand??

我在php文档和堆栈交换中查找了这个问题,并找到了一些建议,但是我似乎无法使它们发挥作用。谁能帮忙吗?

1 个解决方案

#1


16  

If you have the MySql Native Driver extension (mysqlnd), you can use the get_result method to obtain a ResultSet, and then fetch from it the usual way:

如果您有一个MySql本地驱动扩展(mysqlnd),您可以使用get_result方法获得一个ResultSet,然后以通常的方式获取它:

$query = $c->prepare("SELECT e._id,e.description,e.eventDate,e.eventTime,e.address,e.locationDescription,i.guestId,r.guestId IS NOT NULL AS 'RSVP-ed'  FROM eventList AS e  JOIN inviteList AS i ON e._id = i.eventId LEFT JOIN rsvpList AS r ON r.eventId = e._id AND i.guestId = r.guestId JOIN guestList AS g ON g._id = i.guestId WHERE g.groupName = ?");
$query->bind_param('s',$groupName);
$query->execute();
$result = $query->get_result();
$a  = $result->fetch_array(MYSQLI_ASSOC); // this does work :)

推荐阅读
author-avatar
mobiledu2502892717
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有