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 ;
?>