dsrbb20862 2015-04-27 20:03
浏览 298

Wordpress - 更新数据库 - $ wpdb-> prepare

I do have a weird problem inside my custom wordpress script. I want to update my database and I used this line of code to do it:

$update1 = $wpdb->query($wpdb->prepare("UPDATE wp_avg_rate SET avg='$raty' WHERE user_id= '$user->ID'"));

Problem was now, that my page loaded about 20 seconds so I started debbuging and I found out that $wpdb->prepare needs a second parameter. I tried this code and right now it is working:

$update1 = $wpdb->query($wpdb->prepare("UPDATE wp_avg_rate SET avg='$raty' WHERE user_id= %d'", $user->ID));

Problem is now, that in my opinion there is a ' (at the end by %d) closed but never opened so I tried these codes:

$update1 = $wpdb->query($wpdb->prepare("UPDATE wp_avg_rate SET avg='$raty' WHERE user_id= '%d'", $user->ID));

$update1 = $wpdb->query($wpdb->prepare("UPDATE wp_avg_rate SET avg='$raty' WHERE user_id= %d", $user->ID));

As soon as I am using those codes the site load is again more than 20 seconds. Can someone help me and tell me what the correct syntax for the database update is?

  • 写回答

1条回答 默认 最新

  • dongxian5735 2015-04-27 20:59
    关注

    The correct syntax for this SQL query is:

    $update1 = $wpdb->query($wpdb->prepare("UPDATE wp_avg_rate SET avg='$raty' WHERE user_id= %d", $user->ID));
    

    prepare will replace the %d with a sanitised integer of $user->ID

    The 20 second delay you are seeing must be a problem with your database, database server, or your connection to it, which we can't troubleshoot given the information provided.

    Here are things you can try

    • First and foremost make sure that the SQL is getting generated properly. Check out Debug Bar, and the SAVE_QUERIES option for wp-config https://codex.wordpress.org/Debugging_in_WordPress
    • Next Check that you can perform these queries quickly using a MySQL Client. I would recommend using the mysql command line tool if possible but PHPMyAdmin or MySQL Workbench will do the job too
    • Finally if the query is correct and fast when executed in a client, then you might want to check your DNS settings. I have encountered similar problems caused by the Database Server not being able to resolve the WordPress server's hostname

    Please let me know how that goes!

    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么