Last active
June 26, 2022 04:22
-
-
Save Reelix/ecf23fcf356a87dca70912a60741f27a to your computer and use it in GitHub Desktop.
A simple PHP script to test for multiple DB connections in PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// File created by Reelix (HTB: Reelix) | |
// Enter your Host, username, password, database below. | |
$host = "localhost"; | |
$dbuser = "dbusername"; | |
$dbpass = "dbpassword"; | |
$db = ""; | |
$query = ""; | |
if (isset($_GET["db"])) | |
{ | |
$db = $_GET["db"]; | |
} | |
if (isset($_GET["SQL"])) | |
{ | |
$query = $_GET["SQL"]; | |
} | |
TestMySQL($host, $db, $dbuser, $dbpass, $query, $db); | |
TestPosgreSQLNative($host, $db, $dbuser, $dbpass, $query); | |
TestPosgreSQLPDO($host, $db, $dbuser, $dbpass, $query); | |
function TestMySQL($host, $db, $dbuser, $dbpass, $query, $thedb) | |
{ | |
if (function_exists("mysqli_connect")) | |
{ | |
$link = mysqli_connect($host,$dbuser,$dbpass, $thedb); | |
$link = mysqli_connect($host,$dbuser,$dbpass, $thedb); | |
if (mysqli_connect_errno()) | |
{ | |
echo "MySQL - Failed!"; | |
} | |
else | |
{ | |
echo "MySQL - 3306 - Connected!"; | |
$result = mysqli_query($link, $query); | |
while( $row = mysqli_fetch_array($result) ) | |
{ | |
print_r($row); | |
} | |
} | |
} | |
else | |
{ | |
echo "No mysqli_connect - Skipping MySQL<br />"; | |
} | |
} | |
function TestPosgreSQLNative($host, $db, $dbuser, $dbpass, $query) | |
{ | |
if (function_exists("pg_connect")) | |
{ | |
$conn = pg_connect("host=" . $host . " dbname=" . $db . " user=" . $dbuser . " password=" . $dbpass); | |
if($conn) | |
{ | |
echo "PosgreSQLNative - Connected!<br />"; | |
if (isset($query)) | |
{ | |
$result = pg_query($conn, $query); | |
var_dump(pg_fetch_all($result)); | |
} | |
} | |
else | |
{ | |
echo "PosgreSQLNative - Failed!<br />"; | |
} | |
} | |
else | |
{ | |
echo "No pg_connect - Skipping PosgreSQLNative<br />"; | |
} | |
} | |
function TestPosgreSQLPDO($host, $db, $dbuser, $dbpass, $query) | |
{ | |
if (function_exists("PDO")) | |
{ | |
$myPDO = new PDO("pgsql:host=" . $host . ";dbname=" . $db, $dbuser, $dbpass); | |
} | |
else | |
{ | |
echo "No PDO - Skipping PosgreSQLPDO<br />"; | |
} | |
// TODO: Check if it succeeded | |
} | |
// TODO: MSSQL | |
// Try PostGRE | |
/* | |
$query = "SELECT table_name AS table_name FROM information_schema.tables"; | |
$result = mysqli_query($con, $query); | |
while ($row = mysqli_fetch_array($result)){ | |
echo $row['table_name'] . " - "; | |
// echo "Username: " . $row['username'] . " -- " . $row['password'] . " -- " . $row['email'] . " -->Woof"; | |
}*/ | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment