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 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)