I was trying to gather an unknown amount of data from my MySQL database to up load it on my webpage using PHP. But for some reason I kept getting ERR_CONNECTION_RESET in the browser whenever I tried to get more than 1 row of data.
//Connect to database
$Conn = Connect();
//Prepare statement
$SQL = $Conn->prepare("CALL GetProductByID (?)");
$SQL->bind_param("i", $ProductID);
//Loop to get data
foreach ($_SESSION['Cart'] as $ProductID) {
$SQL->execute();
$Result = $SQL->get_result();
while ($Product = $Result->fetch_array(MYSQLI_ASSOC)) {
//Show all items in cart
echo '<div class="product"><h3>Namn: ' . $Product["Name"] . '</h3><p>Pris: ' . $Product["Price"] . ' kr</p><label for="' . $Product["ProductID"] . '">Antal: </label><input type="number" name="' . $Product["ProductID"] . '" placeholder="Antal" min="0" max="150" value="1" required></div>';
}
}
//Close all connections
$SQL->close();
$Conn->close();
The code above would work so long as it only ran the loop once, but as soon as it tried running it a second time the ERR_CONNECTION_RESET issue would appear.
Every time I would try to enter the page and the error occurred the following would be logged in Apaches error log:
[Thu Feb 03 21:56:08.649559 2022] [mpm_winnt:notice] [pid 20304:tid 568] AH00428: Parent: child process 36668 exited with status 3221225477 -- Restarting.
[Thu Feb 03 21:56:08.743608 2022] [ssl:warn] [pid 20304:tid 568] AH01909: www.example.com:443:0 server certificate does NOT include an ID which matches the server name
[Thu Feb 03 21:56:08.754618 2022] [mpm_winnt:notice] [pid 20304:tid 568] AH00455: Apache/2.4.51 (Win64) OpenSSL/1.1.1l PHP/8.0.12 configured -- resuming normal operations
[Thu Feb 03 21:56:08.754618 2022] [mpm_winnt:notice] [pid 20304:tid 568] AH00456: Apache Lounge VS16 Server built: Oct 7 2021 16:27:02
[Thu Feb 03 21:56:08.754618 2022] [core:notice] [pid 20304:tid 568] AH00094: Command line: 'f:\\program\\xampp\\apache\\bin\\httpd.exe -d F:/Program/XAMPP/apache'
[Thu Feb 03 21:56:08.756620 2022] [mpm_winnt:notice] [pid 20304:tid 568] AH00418: Parent: Created child process 25108
[Thu Feb 03 21:56:09.069307 2022] [ssl:warn] [pid 25108:tid 564] AH01909: www.example.com:443:0 server certificate does NOT include an ID which matches the server name
[Thu Feb 03 21:56:09.108307 2022] [ssl:warn] [pid 25108:tid 564] AH01909: www.example.com:443:0 server certificate does NOT include an ID which matches the server name
[Thu Feb 03 21:56:09.121319 2022] [mpm_winnt:notice] [pid 25108:tid 564] AH00354: Child: Starting 150 worker threads.
However, the code below would work without any issues.
//For some reason the connect and close needs to be inside the foreach loop
foreach ($_SESSION['Cart'] as $ProductID) {
//Connect to database
$Conn = Connect();
//Prepare statement
$SQL = $Conn->prepare("CALL GetProductByID (?)");
$SQL->bind_param("i", $ProductID);
$SQL->execute();
$Result = $SQL->get_result();
while ($Product = $Result->fetch_array(MYSQLI_ASSOC)) {
//Show all items in cart
echo '<div class="product"><h3>Namn: ' . $Product["Name"] . '</h3><p>Pris: ' . $Product["Price"] . ' kr</p><label for="' . $Product["ProductID"] . '">Antal: </label><input type="number" name="' . $Product["ProductID"] . '" placeholder="Antal" min="0" max="150" value="1" required></div>';
}
//Close all connections
$SQL->close();
$Conn->close();
}
My question is if there is a way to make my first solution work. It seems an awful waste to me to open and close the entire connection every loop and very much defeats the purpose of a prepared statement.