Skip to content

Commit e62918e

Browse files
committed
Update MySQLConnection.php
MySQLConnection->Close now checks if connection is open before closing
1 parent fceeecc commit e62918e

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

berry/mysql/MySQLConnection.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ class MySQLConnection
44
{
55

66
private mysqli $fLink; // The mysqli link with the selected MySQL database
7+
private bool $fConnectionIsOpen = false;
78

89
/**
910
* Constructs a new MySQLConnection
@@ -19,13 +20,15 @@ public function __construct(string $serverIP, string $database, string $user, st
1920

2021
if ($this->fLink->connect_errno)
2122
{
22-
throw new Exception("Failed to connect to MySQL: (" . $this->fLink->connect_errno . ") " . $this->fLink->connect_error);
23+
throw new Exception("Failed to connect to MySQL:" . $this->fLink->connect_error);
2324
}
2425

2526
if ($charset != "")
2627
{
2728
$this->fLink->set_charset($charset);
2829
}
30+
31+
$this->fConnectionIsOpen = true;
2932
}
3033

3134
/**
@@ -44,7 +47,12 @@ public function EscapeString(string $string): string
4447
*/
4548
public function Close(): void
4649
{
47-
$this->fLink->close();
50+
// Check if connection (fLink) is open before closing it
51+
if ($this->fConnectionIsOpen)
52+
{
53+
$this->fLink->close();
54+
$this->fConnectionIsOpen = false;
55+
}
4856
}
4957

5058
/**
@@ -55,7 +63,6 @@ public function getLink(): mysqli
5563
{
5664
return $this->fLink;
5765
}
58-
5966
}
6067

6168
?>

0 commit comments

Comments
 (0)