doujiu3095 2015-07-14 08:08
浏览 106
已采纳

PHP 5.5.24 MySQLi持久连接返回'无法获取mysqli'

I want to avoid reconnecting database for every query in same page aka want to reuse the connection result once mysqli_connect so I can speed up the results. I tried "p:" in front of hostname. But still I am getting below warnings. I get expected results when I connect without persistent "p:" and connect on every query.

Warning: mysqli_close(): Couldn't fetch mysqli in C:\xampp\htdocs\TodayReport\tr1.php on line 99

Notice: Undefined variable: Result1Con in C:\xampp\htdocs\TodayReport\tr1.php on line 142

Warning: mysqli_close() expects parameter 1 to be mysqli, null given in C:\xampp\htdocs\TodayReport\tr1.php on line 142

I referred below pages.

How to use mysqli persistent connection across different pages

Php Mysqli Persistent connection error

Below is my script (i've minimized to be able to read quickly)

<?php
$webpage = "<!DOCTYPE html><html><head><title>Record Count</title><script src='sorttable.js'></script></head>";
$FirstProjectTitle = "Result1";
$SecondProjectTitle = "Result2";
//constructing HTML layout
Global $LoginDetailsCon;
Global $LoginDetailsResult;
$LoginDetailsCon=mysqli_connect("p:98.168.2.14","root","root@123","resultdbmain") or die("Failed to connect to resultdbmain: " . mysqli_connect_error());
$LoginDetailsQuery="SELECT * FROM login WHERE `IsWorking` = 1 AND `level` = 1";
$LoginDetailsResult=mysqli_query($LoginDetailsCon,$LoginDetailsQuery);
while ($row=mysqli_fetch_array($LoginDetailsResult,MYSQLI_ASSOC)) { 
$Result1Con=mysqli_connect("98.168.2.14","root","root@123","resultdb1") or die("Failed to connect to resultdb1: " . mysqli_connect_error());
$result1Query="SELECT * FROM `resultdb1table1` WHERE `SID` = " . $row["ID"] ." AND `KeyDate` > '" . date("Y-m-d") . " 00:00:00' ";
$result1Result=mysqli_query($Result1Con,$result1Query);
$i=0;
$ik=0;
$SName = $row["Name"];
while ($row=mysqli_fetch_array($result1Result,MYSQLI_ASSOC)) { 
$i++;
$ik = $ik + $row["KeyStroke"];
if ($i==1) {
$started=$row["KeyDate"];
}
$Lastkeyed=$row["KeyDate"];
}
if ($i > 0) {
//constructing HTML layout with results
}
}
$webpage .= "</table>";
mysqli_close($Result1Con);
//$LoginDetailsCon=mysqli_connect("98.168.2.14","root","root@123","resultdbmain");
//if (mysqli_connect_errno()) {  echo "Failed to connect to resultdbmain: " . mysqli_connect_error(); }
//$LoginDetailsQuery="SELECT * FROM login WHERE `IsWorking` = 1 AND `level` = 1";
//$LoginDetailsResult=mysqli_query($LoginDetailsCon,$LoginDetailsQuery);
while ($row=mysqli_fetch_array($LoginDetailsResult,MYSQLI_ASSOC)) { 
$Result2Con=mysqli_connect("98.168.2.14","root","root@123","resultdb2") or die("Failed to connect to resultdb2: " . mysqli_connect_error());
$result2Query="SELECT * FROM `resultdb2table1` WHERE `ID` = " . $row["ID"] ." AND `KeyDate` > '" . date("Y-m-d") . " 00:00:00' ";
$result2Result=mysqli_query($result2Con,$result2Query);
$i=0;
$ik=0;
$UName = $row["Name"];
while ($row=mysqli_fetch_array($result2Result,MYSQLI_ASSOC)) { 
$i++;
$ik = $ik + $row["KeyStroke"];
if ($i==1) {
$started=$row["KeyDate"];
}
$Lastkeyed=$row["KeyDate"];
}
if ($i > 0) {
//constructing HTML layout with results
}
}
mysqli_close($result2Con);
mysqli_close($LoginDetailsCon);
$webpage .= "</body></html>";
echo $webpage ;
?>
  • 写回答

1条回答 默认 最新

  • douxi1738 2015-07-14 08:20
    关注

    Your idea on database connections is quite err... unusual.

    Surely you don't have to reconnect for every query in same page. But you don't need multiple connections for this either. Just use one single connection. As a matter of fact, you don't have to (and should not) connect every time you runs a query. Instead, you should connect only once per script execution, and then use that single connection variable all the way through your code.

    So, create a file called connection.php, place your connection code there once, and then include it in every script that requires database interaction.

    Also, do not call mysqli_close(). PHP will close connection for you.

    And forget about persistent connections for a while. You don't need it.

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

报告相同问题?

悬赏问题

  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计