dongtanlin0765 2015-05-25 13:25
浏览 57
已采纳

mysql命令不同步(我使用mysql而不是mysqli!)[关闭]

today i faced a strange error witch i didn't faced before !

i Google it but not helped .

in here i found some problem like me BUT it was mysqli !

i am using mysql only not mysqli.

mysql_query("SELECT id FROM s1_users WHERE id>4 AND ".time()." - timestamp < 300");

but i get

mysql commands out of sync you can't run this command now

i ran this query's hundred time before worked fine but i don't know why its telling me this error.

  • 写回答

1条回答 默认 最新

  • douzhuiqiu4923 2015-05-25 13:41
    关注

    Q: What causes this "strange error" to be returned?

    mysql commands out of sync you can't run this command now


    A: That error isn't strange. It's expected behavior.

    Reference: http://dev.mysql.com/doc/refman/5.5/en/commands-out-of-sync.html

    If you get Commands out of sync; you can't run this command now in your client code, you are calling client functions in the wrong order.


    EDIT

    Q: How do I debug my program?

    A: Check the return from your database interface calls for errors.

    Reference: How to debug small programs http://ericlippert.com/2014/03/05/how-to-debug-small-programs/

    Make just a small change your code. Generate the SQL text into a string variable as a separate step, before prepare/execute of the generated SQL text. And before the prepare/execute, output the string containing the actual SQL text that's going to be submitted, e.g.

    $sql = "SELECT id FROM s1_users WHERE id>4 AND ".time()." - timestamp < 300";
    echo "SQL=" . $sql;  // for debugging
    

    Then use a reference to $sql into your database interface function call (e.g. prepare or execute), pass the SQL statement to the database server. Here's a good pattern to follow: whenever you generate dynamic SQL and submit it to the database, do it in two steps. One step to generate the string, and a separate step that submits the generated SQL to the database.

    When you run the code, the "echo" (or vardump or whatever) will show you the actual SQL text that's going to be submitted to the database. You can take that SQL statement to a different client, and test it. Likely, the database server is throwing an error on that statement, and you'll be able to spot it.

    It's also likely that your code (which you haven't shown us) isn't checking the return from the database interface calls, to see if there was an error or not. I suspect your code is putting it's pinky finger to the corner of its mouth, Dr. Evil style, and saying "I'm just gonna assume it all went to plan. What?"


    FOLLOWUP

    Why do you need to supply the value from the PHP time() function as part of the SQL text? If you need the current time from the database server, the MySQL NOW() function would give that to you. If you need a unix timestamp version of that value (seconds since the beginning of the era in UTC), you can use the MySQL UNIX_TIMESTAMP function, e.g. UNIX_TIMESTAMP(NOW()).

    If you can have a static string as a SQL statement that achieves what you need, that is usually a better option. If you need to supply literal values in a SQL statement, a better pattern for doing that is to use a prepared statement with bind placeholders.

    (Yes, there's a time and a place for everything, including dynamically generated SQL. But looking at what you are trying to achieve with your code, that time and place is never and nowhere.)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题