mysql - PHP 5.5.24 MySQLi persistent connection returns 'couldn't fetch mysqli' -


i want avoid reconnecting database every query in same page aka want reuse connection result once mysqli_connect can speed results. tried "p:" in front of hostname. still getting below warnings. expected results when connect without persistent "p:" , 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 mysqli, null given in c:\xampp\htdocs\todayreport\tr1.php on line 142

i referred below pages.

how use mysqli persistent connection across different pages

php mysqli persistent connection error

below script (i've minimized able 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 connect resultdbmain: " . mysqli_connect_error()); $logindetailsquery="select * login `isworking` = 1 , `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 connect resultdb1: " . mysqli_connect_error()); $result1query="select * `resultdb1table1` `sid` = " . $row["id"] ." , `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 results } } $webpage .= "</table>"; mysqli_close($result1con); //$logindetailscon=mysqli_connect("98.168.2.14","root","root@123","resultdbmain"); //if (mysqli_connect_errno()) {  echo "failed connect resultdbmain: " . mysqli_connect_error(); } //$logindetailsquery="select * login `isworking` = 1 , `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 connect resultdb2: " . mysqli_connect_error()); $result2query="select * `resultdb2table1` `id` = " . $row["id"] ." , `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 results } } mysqli_close($result2con); mysqli_close($logindetailscon); $webpage .= "</body></html>"; echo $webpage ; ?> 

your idea on database connections quite err... unusual.

surely don't have reconnect every query in same page. don't need multiple connections either. use 1 single connection. matter of fact, don't have (and should not) connect every time runs query. instead, should connect only once per script execution, , use single connection variable way through code.

so, create file called connection.php, place connection code there once, , include in every script requires database interaction.

also, not call mysqli_close(). php close connection you.

and forget persistent connections while. don't need it.


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -