以自定义格式显示PDO Fecth All Colum

时间:2019-01-06 11:18:25

标签: php pdo

我有一个PDO fecth函数,代码是

java.lang.NullPointerException
    at com.travelplannr.endpoint.api.TravelPlannrApi.addUser(TravelPlannrApi.java:34)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.google.appengine.tools.development.agent.runtime.Runtime.invoke(Runtime.java:129)
    at com.google.api.server.spi.SystemService.invokeServiceMethod(SystemService.java:351)
    at com.google.api.server.spi.handlers.EndpointsMethodHandler$RestHandler.handle(EndpointsMethodHandler.java:119)
    at com.google.api.server.spi.handlers.EndpointsMethodHandler$RestHandler.handle(EndpointsMethodHandler.java:102)
    at com.google.api.server.spi.dispatcher.PathDispatcher.dispatch(PathDispatcher.java:49)
    at com.google.api.server.spi.EndpointsServlet.service(EndpointsServlet.java:71)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1166)
    at com.google.appengine.api.socket.dev.DevSocketFilter.doFilter(DevSocketFilter.java:72)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at com.google.appengine.tools.development.ResponseRewriterFilter.doFilter(ResponseRewriterFilter.java:134)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at com.google.appengine.tools.development.HeaderVerificationFilter.doFilter(HeaderVerificationFilter.java:34)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at com.google.appengine.api.blobstore.dev.ServeBlobFilter.doFilter(ServeBlobFilter.java:63)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at com.google.apphosting.utils.servlet.TransactionCleanupFilter.doFilter(TransactionCleanupFilter.java:48)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at com.google.appengine.tools.development.StaticFileFilter.doFilter(StaticFileFilter.java:122)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at com.google.appengine.tools.development.DevAppServerModulesFilter.doDirectRequest(DevAppServerModulesFilter.java:366)
    at com.google.appengine.tools.development.DevAppServerModulesFilter.doDirectModuleRequest(DevAppServerModulesFilter.java:349)
    at com.google.appengine.tools.development.DevAppServerModulesFilter.doFilter(DevAppServerModulesFilter.java:116)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at com.google.appengine.tools.development.DevAppServerRequestLogFilter.doFilter(DevAppServerRequestLogFilter.java:44)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:388)
    at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
    at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)
    at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765)
    at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:418)
    at com.google.appengine.tools.development.DevAppEngineWebAppContext.handle(DevAppEngineWebAppContext.java:95)
    at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
    at com.google.appengine.tools.development.JettyContainerService$ApiProxyHandler.handle(JettyContainerService.java:508)
    at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
    at org.mortbay.jetty.Server.handle(Server.java:326)
    at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542)
    at org.mortbay.jetty.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:923)
    at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:547)
    at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:212)
    at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)
    at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:409)
    at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:582)

并使用此代码在我的索引中调用

function getAllUserTicketHistoryJson($rid){
    global $conn;
    $stmt = $conn->prepare("SELECT user_id, total_ticket FROM ticket_user WHERE round_id = :rou");
    $stmt->bindParam(':rou', $rid);
    $stmt->execute();
    $res = $stmt->fetchAll(PDO::FETCH_ASSOC);
    return $res;
}

输出显示如下

  

注意:...中的数组到字符串的转换

我想要的是显示所有这样的获取列。

$getAllUserTicketHistoryJson = getAllUserTicketHistoryJson($getTicket['id']);

有人可以帮助我吗?

编辑

为什么我要像这样的代码格式

$array=array('key' => value, 'key' => value, 'key' => value, 'key' => value);

因为我想这样使用它

$array=array('user_id' => total_ticket, 'user_id' => total_ticket, 'user_id' => total_ticket, 'user_id' => total_ticket);

它将在我的下一个代码中使用

$array=array('2' => 75.76, '3' => 15.15, '4' => 7.58, '1' => 1.52);

1 个答案:

答案 0 :(得分:0)

您的函数返回一个数组,echo将输出一个字符串,但不能用于数组。

当您尝试以Json格式返回结果时,返回时应该对响应进行json编码

function getAllUserTicketHistoryJson($rid){
    global $conn;
    $stmt = $conn->prepare("SELECT user_id, total_ticket FROM ticket_user WHERE round_id = :rou");
    $stmt->bindParam(':rou', $rid);
    $stmt->execute();
    $res = $stmt->fetchAll(PDO::FETCH_ASSOC);
    return json_encode($res);
}

json_encode()将返回一个json字符串而不是一个数组,因此您将能够对此进行回显。

如果要输出数组的内容(而不是json字符串),则应使用var_dump()进行操作。