【问题标题】:PHP for loop mysql array values insert - inserting 100 000 rows instead of array lengthPHP for循环mysql数组值插入-插入100 000行而不是数组长度
【发布时间】:2011-05-30 13:13:42
【问题描述】:

问题应该很清楚,但我会再解释一次: 我正在尝试从 mysql 数据库中的数组中插入单个值。

代码如下:

public function addRoute($pointArray){

    $length = 4; //just to be sure that nothing went wrong with calulating the length

    for($i = 0; $i < $length; $i++){

        $result = "INSERT into route_point(latlng, routeID) VALUES (4849, 10)"; //sample values, to once again be sure that there is nothing wrong with the values
        mysql_query($result);
    }

    mysql_close(); //just to be sure again
    return true;
}

如果有人想知道,pointArray 是使用 zendamf 框架从 actionscript 传递的。在我看来,该语句没有任何问题,但它不是插入 4 行,而是插入超过 100 000 行。我也没有在 actionscript 中收到“true”,它应该在执行 php 函数时传递.

我也尝试了 for each 循环,但我得到一个错误(我无法读取,因为我从 actionscript 调用 php 文件)。

foreach ($pointArray as $value) {

        $result = "INSERT into route_point(latlng, routeID) VALUES ('$value', 10)";
        mysql_query($result);

    }

不过我更喜欢使用 for 循环。

对此一无所知,现在摆弄了几个小时的几个设置。

【问题讨论】:

  • Actionscript 不支持“稀疏”数组进行输出。如果您在 AS 中执行 arr[500] = 1,然后通过 AMF 或 JSON 或其他方式将其发送出去,则输出数据将使用定义的数组元素 1 到 499 构建,而不仅仅是 #500,因此您发送的是 500 个元素,不是 1.
  • 我已经能够通过 zendamf 使用 Chrome 开发者工具的网络选项卡查看 PHP 错误。它有助于在 Zend_Amf_Server 实例上运行 $server-&gt;setProduction(!DEBUG)

标签: php mysql arrays loops for-loop


【解决方案1】:

你确定addRoute 只被调用一次吗? PHP文件只被调用一次?我会问$pointArray 中有什么,但在这种情况下这并不重要 =)

您可以在addRoute 函数中添加日志(写入文件$file=microtime(1).'.log' 以查看它被调用的频率。您可以在addRoute 循环中构建日志内容(每个addRoute 调用):

public function addRoute($pointArray){

$length = 4; 
//just to be sure that nothing went wrong with calulating the length

$log = '';
for($i = 0; $i &lt; $length; $i++){

    $log .= $i."\n"

    $result = "INSERT into route_point(latlng, routeID) VALUES (4849, 10)"; 
    //sample values, 
    //to once again be sure that there is nothing wrong with the values
    mysql_query($result);
}

file_put_contents(microtime(1).'.log', $log);

mysql_close(); //just to be sure again
return true;
}

然后看看到底发生了什么=)

【讨论】:

  • 谢谢!错误已解决,我不知道如何但它突然起作用了 o_O 它与 actionscript 数组和 php 数组之间的转换有关。无论如何,下一个问题是在循环中,发送 $pointArray[$i] 返回一个错误(“插入到 route_point(latlng)值('$pointArray[$i]')”)。使用 $pointArray[0] 可以正常工作,$i 部分有问题。
  • 您可能还想将 mysql_error() 的结果添加到日志中。甚至更好:学习使用调试器(Xdebug 等)。 es 可以与此类服务相结合
  • 我去看看。看不到具体的错误信息很烦人。
猜你喜欢
  • 2012-06-19
  • 2012-06-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-23
  • 2017-12-17
相关资源
最近更新 更多