PHP在MySQL查询中随机丢弃变量

时间:2014-07-17 09:50:59

标签: php mysql

我遇到了一个奇怪的PHP问题让我感到有点难过。我正在从循环(在var $user_query中)构建一个插入查询字符串,然后执行以下代码:

    $user_query="INSERT IGNORE INTO user (id,user_name) VALUES ".$user_query; 
    $user_query=substr($user_query,0,-1);
    echo $user_query ."\n";
    mysql_query($user_query);
    if(mysql_error()) {$error=$user_query . mysql_error(); echo $error; exit;}
    unset($user_query);

然后我运行它:

    shell> php ingest.php 
    INSERT IGNORE INTO user (id,user_name) VALUES (24462186,'msbelindabee'),(478099020,'beenthat'),(294153799,'sh3lly05'),(22700530,'srcaino'),(755684,'hoony_ky'),(188148256,'judith_prietht'),(14687197,'realnicolezavala'),(467409503,'nicolezavalax3'),(35229813,'akaomar91091'),(560376223,'christophernineteen'),(1394834191,'punkpipes')
    INSERT IGNORE INTO user (id,user_name) VALUES
    INSERT IGNORE INTO user (id,user_name) VALUESYou have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1 
    shell> 

似乎$user_query var在回显和调用mysql_query()函数之间的某个时间被截断。我已经在这个剧本中的其他几个地方发生过这种情况。我很难过 - 这可能是记忆问题吗?我没有收到任何内存错误。

我检查了apache和mysql错误日志 - 没有记录任何内容。

2 个答案:

答案 0 :(得分:1)

在循环中发生这种情况时非常有意义。第一次进入循环时,变量$ user_query可能包含您要插入的值。 查询正确回显并可能正确执行(在数据库中检查)。 然后取消设置$ user_query变量。

在下一次迭代中,您输入的$ user_query变量中没有任何内容,所以

$user_query="INSERT IGNORE INTO user (id,user_name) VALUES ".$user_query;

不会向该查询附加任何内容。

echo和执行之间没有任何反应,这段代码执行两次并在第二次运行时产生错误。

答案 1 :(得分:0)

可能需要增加MySQL中的max_allowed_pa​​cket

shell> mysql --max_allowed_packet=32M

http://dev.mysql.com/doc/refman/5.0/en/packet-too-large.html