Skip to content

Commit 43192d2

Browse files
authored
chore: Merge pull request #118 from WebFiori/dev
docs: Updated Code Examples
2 parents 963743b + ce42d85 commit 43192d2

File tree

5 files changed

+971
-460
lines changed

5 files changed

+971
-460
lines changed
Lines changed: 66 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,66 @@
1-
<?php
2-
3-
require_once '../../vendor/autoload.php';
4-
5-
use WebFiori\Database\ConnectionInfo;
6-
use WebFiori\Database\Database;
7-
8-
echo "=== WebFiori Database Connection Example ===\n\n";
9-
10-
try {
11-
// Create connection info
12-
$connection = new ConnectionInfo('mysql', 'root', '123456', 'mysql');
13-
echo "✓ Connection info created\n";
14-
echo " Database Type: ".$connection->getDatabaseType()."\n";
15-
echo " Host: ".$connection->getHost()."\n";
16-
echo " Database: ".$connection->getDBName()."\n\n";
17-
18-
// Establish database connection
19-
$database = new Database($connection);
20-
echo "✓ Database connection established\n";
21-
22-
// Test connection with a simple query
23-
$result = $database->setQuery("SELECT VERSION() as version")->execute();
24-
25-
if ($result) {
26-
echo "✓ Connection test successful\n";
27-
$rows = $result->getRows();
28-
29-
if (!empty($rows)) {
30-
echo " MySQL Version: ".$rows[0]['version']."\n";
31-
}
32-
}
33-
} catch (Exception $e) {
34-
echo "✗ Error: ".$e->getMessage()."\n";
35-
echo "Note: Make sure MySQL is running and accessible with the provided credentials.\n";
36-
}
37-
38-
echo "\n=== Example Complete ===\n";
1+
<?php
2+
3+
require_once '../../vendor/autoload.php';
4+
5+
use WebFiori\Database\ConnectionInfo;
6+
use WebFiori\Database\Database;
7+
8+
echo "=== WebFiori Database Connection Example ===\n\n";
9+
10+
try {
11+
// Create connection info
12+
$connection = new ConnectionInfo('mysql', 'root', '123456', 'mysql');
13+
echo "✓ Connection info created\n";
14+
echo " Database Type: ".$connection->getDatabaseType()."\n";
15+
echo " Host: ".$connection->getHost()."\n";
16+
echo " Database: ".$connection->getDBName()."\n\n";
17+
18+
// Establish database connection
19+
$database = new Database($connection);
20+
echo "✓ Database connection established\n";
21+
22+
// Test connection with a simple query using raw()
23+
$result = $database->raw("SELECT VERSION() as version")->execute();
24+
25+
if ($result) {
26+
echo "✓ Connection test successful\n";
27+
$rows = $result->getRows();
28+
29+
if (!empty($rows)) {
30+
echo " MySQL Version: ".$rows[0]['version']."\n";
31+
}
32+
}
33+
34+
// Additional connection tests using raw() with parameters
35+
echo "\n--- Additional Connection Tests ---\n";
36+
37+
// Test current database
38+
$result = $database->raw("SELECT DATABASE() as current_db")->execute();
39+
if ($result && $result->getRowsCount() > 0) {
40+
echo "✓ Current database: " . $result->getRows()[0]['current_db'] . "\n";
41+
}
42+
43+
// Test server status
44+
$result = $database->raw("SHOW STATUS LIKE 'Uptime'")->execute();
45+
if ($result && $result->getRowsCount() > 0) {
46+
$uptime = $result->getRows()[0]['Value'];
47+
echo "✓ Server uptime: " . $uptime . " seconds\n";
48+
}
49+
50+
// Test connection info
51+
$result = $database->raw("SELECT CONNECTION_ID() as connection_id")->execute();
52+
if ($result && $result->getRowsCount() > 0) {
53+
echo "✓ Connection ID: " . $result->getRows()[0]['connection_id'] . "\n";
54+
}
55+
56+
$result = $database->raw("SELECT USER() as user_name")->execute();
57+
if ($result && $result->getRowsCount() > 0) {
58+
echo "✓ Current User: " . $result->getRows()[0]['user_name'] . "\n";
59+
}
60+
61+
} catch (Exception $e) {
62+
echo "✗ Error: ".$e->getMessage()."\n";
63+
echo "Note: Make sure MySQL is running and accessible with the provided credentials.\n";
64+
}
65+
66+
echo "\n=== Example Complete ===\n";

0 commit comments

Comments
 (0)