Skip to content

Commit 5e545a1

Browse files
authored
Merge pull request #11 from jr-cologne/jr_add-connected-method
Add functionality to check whether connection to database is established
2 parents b5099c5 + ad4d0dc commit 5e545a1

File tree

6 files changed

+70
-6
lines changed

6 files changed

+70
-6
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,20 @@ if ($db->connect('mysql:host=localhost;dbname=db-class-example;charset=utf8', 'r
7676
}
7777
```
7878

79+
### Checking Connection to Database
80+
81+
You can also check the connection to the database by the method `DB::connected()` after connecting.
82+
83+
Example:
84+
85+
```php
86+
if ($db->connected()) {
87+
echo 'Successfully connected to database';
88+
} else {
89+
echo 'Connection failed';
90+
}
91+
```
92+
7993
### Retrieving Data from Database
8094

8195
In order to retrieve data from a database, you need to walk through the following three steps:

composer.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "jr-cologne/db-class",
33
"type": "library",
4-
"version": "2.0.1",
4+
"version": "2.1.0",
55
"description": "A simple database class with PHP, PDO and a query builder.",
66
"keywords": [
77
"database",
@@ -19,7 +19,7 @@
1919
}
2020
},
2121
"homepage": "https://github.com/jr-cologne/db-class",
22-
"time": "2017-10-21",
22+
"time": "2017-11-10",
2323
"license": "MIT",
2424
"authors": [
2525
{
@@ -31,7 +31,7 @@
3131
],
3232
"support": {
3333
"issues": "https://github.com/jr-cologne/db-class/issues",
34-
"source": "https://github.com/jr-cologne/db-class/tree/v2.0.1",
34+
"source": "https://github.com/jr-cologne/db-class/tree/v2.1.0",
3535
"docs": "https://github.com/jr-cologne/db-class/blob/README.md"
3636
}
3737
}

src/API.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,17 @@ JRCologne\Utils\Database
4747
```
4848

4949

50+
#### `$connected`
51+
52+
**Description:** Defines whether a connection to a database is established.
53+
54+
**Visibility:** `protected`
55+
56+
**Data Type:** `boolean`
57+
58+
**Default Value:** `false`
59+
60+
5061
#### `$results`
5162

5263
**Description:** The results of the query from the method `DB::select()`.
@@ -126,6 +137,23 @@ Creates a connection to a specified database by PDO.
126137
**Return Values:** `boolean` true on success, false on failure
127138

128139

140+
#### `DB::connected()`
141+
142+
**Description:**
143+
144+
```php
145+
DB::connected()
146+
```
147+
148+
Checks whether a connection to a database is established.
149+
150+
**Visibility:** `public`
151+
152+
**Parameters:** -
153+
154+
**Return Values:** `boolean`
155+
156+
129157
#### `DB::table()`
130158

131159
**Description:**

src/DB.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* @author JR Cologne <[email protected]>
1414
* @copyright 2017 JR Cologne
1515
* @license https://github.com/jr-cologne/db-class/blob/master/LICENSE MIT
16-
* @version v2.0.1
16+
* @version v2.1.0
1717
* @link https://github.com/jr-cologne/db-class GitHub Repository
1818
* @link https://packagist.org/packages/jr-cologne/db-class Packagist site
1919
*
@@ -53,6 +53,17 @@ class DB extends PDO {
5353
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
5454
];
5555

56+
/**
57+
* Defines whether a connection to a database is established.
58+
*
59+
* Value:
60+
* true if a connection to a database is established,
61+
* false if a connection to a database isn't established (default)
62+
*
63+
* @var boolean
64+
*/
65+
protected $connected = false;
66+
5667
/**
5768
* The results of the query from the method DB::select().
5869
*
@@ -108,12 +119,23 @@ public function connect(string $dsn, string $username = null, string $password =
108119
}
109120
} catch (PDOException $e) {
110121
$this->pdo_exception = $e;
122+
$this->connected = false;
111123
return false;
112124
}
113125

126+
$this->connected = true;
114127
return true;
115128
}
116129

130+
/**
131+
* Checks whether a connection to a database is established.
132+
*
133+
* @return boolean
134+
*/
135+
public function connected() {
136+
return $this->connected;
137+
}
138+
117139
/**
118140
* Sets the table to use for the following query.
119141
*

src/Exceptions/UnsupportedKeywordException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* @author JR Cologne <[email protected]>
1414
* @copyright 2017 JR Cologne
1515
* @license https://github.com/jr-cologne/db-class/blob/master/LICENSE MIT
16-
* @version v2.0.1
16+
* @version v2.1.0
1717
* @link https://github.com/jr-cologne/db-class GitHub Repository
1818
* @link https://packagist.org/packages/jr-cologne/db-class Packagist site
1919
*

src/QueryBuilder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* @author JR Cologne <[email protected]>
1414
* @copyright 2017 JR Cologne
1515
* @license https://github.com/jr-cologne/db-class/blob/master/LICENSE MIT
16-
* @version v2.0.1
16+
* @version v2.1.0
1717
* @link https://github.com/jr-cologne/db-class GitHub Repository
1818
* @link https://packagist.org/packages/jr-cologne/db-class Packagist site
1919
*

0 commit comments

Comments
 (0)