【问题标题】:[] operator not supported for strings within php web servicephp Web 服务中的字符串不支持 [] 运算符
【发布时间】:2018-04-20 13:16:01
【问题描述】:

这和其他骗子不太一样!

我有一个基于传入令牌获取数据的 Web 服务

$query = "SELECT * FROM Journey WHERE Token='{$token}';";
$result = mysql_query($query,$link) or die('Errant query:  '.$query);
$posts = array();
$dto = new Journey();
$dta = new Dta();
if(mysql_num_rows($result) > 0) {
  while($post = mysql_fetch_assoc($result)) {
    $posts[] = $post;
    $posts = array_shift($posts);

当我运行服务并传入有效令牌时,我在$posts[] = $post 处收到致命错误

据我所见(并已阅读),这看起来是正确的方法(事实上,我在其他 Web 服务中有它并且它在那里工作)

使用 PHP 5.6.3

【问题讨论】:

  • 你不应该再使用那些旧的mysql_ 函数了。您是否访问过 php 手册以了解如何从结果集中收集数据?
  • 您收到的确切错误消息是什么? @mickmackusa 是正确的,mysql_ 函数已不再被推荐。
  • 差不多...stackoverflow.com/questions/42390548/…我会继续寻找
  • @ThisGuyHasTwoThumbs “字符串不支持 [] 运算符”
  • 实际错误在标题中给出——字符串不支持[]运算符

标签: php


【解决方案1】:

$posts = array_shift($posts); 中,您使用数组$posts 的第一个元素(字符串)覆盖数组,因此在下一个循环周期$posts[] = $post; 会发出上述错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-07-02
    • 2010-09-17
    • 2011-08-18
    • 1970-01-01
    • 1970-01-01
    • 2021-11-09
    • 2018-07-10
    • 2023-03-26
    相关资源
    最近更新 更多