gpt4 book ai didi

javascript - 在对 php 文件的调用之间维护 mysql 连接

转载 作者:行者123 更新时间:2023-11-29 00:21:47 24 4
gpt4 key购买 nike

如何在调用外部 .php 文件之间保持 mysql 连接?每次重新连接到数据库时,我都失去了将近 2 秒的时间,这太多了。顺便说一句,我听说持久连接很糟糕,所以我正在寻找替代方案,除非你说服我它们没问题。

在 home.php 我有:

function GetCardSuggestionList(firstChars) {
$.ajax({
type: "POST",
url: 'GetCardList.php',
datatype: 'text',
data: ({FirstChars: firstChars}),
success: function(suggestionList) {
ShowSuggestionList(eval(suggestionList));
}
});
}

在 GetCardList.php 中,我有:

<?php

$cardFirstChars = $_POST['FirstChars'];
$foundCardList = array();

$dbCon=mysqli_connect("host", "user", "pass", "db");
$queryResult = mysqli_query($dbCon, "SELECT name FROM card WHERE name LIKE '%" . $cardFirstChars . "%'");

while($suggList = mysqli_fetch_array($queryResult)) {
array_push($foundCardList, $suggList['name']);
}

echo json_encode( $foundCardList );

?>

更新: 虽然我选择的最佳答案实际上并没有回答问题,但它让我摆脱了我的根本问题,这是一个速度问题。

最佳答案

尝试使用像 127.0.0.1 这样的 ip 地址作为本地主机或服务器 ip 地址,而不是本地主机或主机名。对我来说,这将我的数据库连接速度从几秒钟加快到眨眼。

这似乎是一个问题,尤其是在 Windows 上

关于javascript - 在对 php 文件的调用之间维护 mysql 连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20788429/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com