PHP的mysqli - 从prepared语句返回一个关联数组 [英] PHP mysqli - return an associative array from a prepared statement

查看:209
本文介绍了PHP的mysqli - 从prepared语句返回一个关联数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用的mysqli以prepare声明要想在变量值安全地传递给查询。所有这一切都为我工作,但我快到的问题是获得结果的关联数组。
这里是我的结构至今:

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:

$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.

在最重要的是,最终的目标是一个JSON EN codeD关联数组传回给我的应用程序的其余部分。

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

我抬头问题上的PHP文件中并在堆栈中交流我发现的建议,但我似乎无法让他们的工作。任何人都可以伸出援助之手?

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??

推荐答案

您错过了$结果= $查询 - > get_result();

You missed $result = $query->get_result();

$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 ($query->execute()){
    $result = $query->get_result();
    $a  = $result->fetch_array(MYSQLI_ASSOC); // this does work :)
} else{
    error_log ("Didn't work");
}

这篇关于PHP的mysqli - 从prepared语句返回一个关联数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆