简体   繁体   English

选择查询缓慢更新

[英]Select query slow to update

This is probably a simple error, but I'm stumped... 这可能是一个简单的错误,但我很困惑...

I have a database that has entries inserted using AJAX on a chrome extension, which works great and inserts instantly. 我有一个数据库,其中包含在chrome扩展程序上使用AJAX插入的条目,该数据库很好用,并且可以立即插入。

I have a separate PHP file that is being used to output the number of entries in a table. 我有一个单独的PHP文件,该文件用于输出表中的条目数。 This works, but is takes a long time for the entries to update to the right number when called. 此方法有效,但调用时将条目花费很长时间才能更新为正确的号码。 I need it to be up to date instantly. 我需要立即更新。

Is there any reason why it's taking so long for the query to output the correct number, when the table itself is updating instantly? 当表本身立即更新时,查询输出正确的数字花这么长时间有什么原因吗?

<?php
$link = mysqli_connect("localhost", "root", "", "fyp");

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}
if ($result = mysqli_query($link, "SELECT * FROM links")) {

    $row_cnt = mysqli_num_rows($result);

    printf($row_cnt);

    mysqli_free_result($result);
}

/* close connection */
mysqli_close($link);
?>

Thanks. 谢谢。

I think you should simple run: 我认为您应该简单运行:

if ($result = mysqli_query($link, "SELECT count(*) AS `nr` FROM links")) {

    $row_cnt = mysqli_fetch_assoc($result);

    printf($row_cnt['nr']);

    mysqli_free_result($result);
}

It will be the best one. 这将是最好的。 Or if your links are not removed at all you could run: 或者,如果根本不删除链接,则可以运行:

if ($result = mysqli_query($link, "SELECT id FROM links ORDER BY id DESC LIMIT 1")) {

    $row_cnt = mysqli_fetch_assoc($result);

    printf($row_cnt['id']);

    mysqli_free_result($result);
}

Hand off the processing to the database by using an aggregate query and return a scalar result with mysqli_result : 通过使用聚合查询将处理mysqli_result给数据库,并使用mysqli_result返回标量结果:

if ($result = mysqli_query($link, "SELECT COUNT(1) FROM links")) {
    $row_cnt = mysqli_result($result, 0);
    printf($row_cnt);
    mysqli_free_result($result);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM