<?php
class dbConnector {
private $db;
function __construct($host, $name, $pw, $database) {
$db = mysqli_connect($host, $name, $pw ) or die("Could not obtain initial connection");
if(!$db) {
throw new Exception("Could not obtain database connection", 1);
exit;
}
if (!mysqli_select_db($db, $database)){
throw new Exception("Failed to produce db connection", 2);
exit;
}
}
function SelectQueryDB($queryString) {
$selectResult=mysqli_fetch_array(mysqli_query($db, $queryString));
if (!$selectResult) {
throw new Exception("Failed database query", 3);
} else {
return $selectResult;
}
}
function InsertQueryDB($queryString) {
$insertResult=mysqli_fetch_array(mysqli_query($db, $queryString));
if (!$insertResult) {
throw new Exception("Failed database query", 4);
} else {
return $insertResult;
}
}
function UpdateQueryDB($queryString) {
$updateResult=mysqli_fetch_array(mysqli_query($db, $queryString));
if (!$updateResult) {
throw new Exception("Failed database query", 5);
} else {
return $updateResult;
}
}
function DeleteQueryDB($queryString) {
$deleteResult=mysqli_fetch_array(mysqli_query($db, $queryString));
if (!$deleteResult) {
throw new Exception("Failed database query", 6);
} else {
return $deleteResult;
}
}
function ShowDBresource() {
// echo $db."<br />";
if (isset($db)) {
return true;
} else {
return false;
}
}
}
And here is my test code that is using the class:
<?php
require('dbConnector.php');
//Make a new database connection object
$database = new dbConnector("localhost", "scriptuser", "scriptpassword", "sampledb");
//check the status of the $db resource
$showResult = $database->ShowDBresource();
if ($showResult) {
echo "Resource was set!";
} else {
echo "Resource was NOT set!";
}
//Try using a SELECT query
$results = $database->SelectQueryDB("SELECT * FROM players WHERE name='Testname'");
$toWrite = $results['name'];
$toWrite2 = $results['security'];
echo "Name: $toWrite -- Security: $toWrite2";
?>
And this is the error message I am getting:
Quote
Resource was NOT set!
Notice: Undefined variable: db in C:\wamp\www\test\dbconnector\dbConnector.php on line 40
Warning: mysqli_query() expects parameter 1 to be mysqli, null given in C:\wamp\www\test\dbconnector\dbConnector.php on line 40
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in C:\wamp\www\test\dbconnector\dbConnector.php on line 40
Fatal error: Uncaught exception 'Exception' with message 'Failed database query' in C:\wamp\www\test\dbconnector\dbConnector.php:42 Stack trace: #0 C:\wamp\www\test\dbconnector\dbtest.php(18): dbConnector->SelectQueryDB('SELECT * FROM p...') #1 {main} thrown in C:\wamp\www\test\dbconnector\dbConnector.php on line 42
Notice: Undefined variable: db in C:\wamp\www\test\dbconnector\dbConnector.php on line 40
Warning: mysqli_query() expects parameter 1 to be mysqli, null given in C:\wamp\www\test\dbconnector\dbConnector.php on line 40
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in C:\wamp\www\test\dbconnector\dbConnector.php on line 40
Fatal error: Uncaught exception 'Exception' with message 'Failed database query' in C:\wamp\www\test\dbconnector\dbConnector.php:42 Stack trace: #0 C:\wamp\www\test\dbconnector\dbtest.php(18): dbConnector->SelectQueryDB('SELECT * FROM p...') #1 {main} thrown in C:\wamp\www\test\dbconnector\dbConnector.php on line 42
I thought because I created the $db variable outside of a function that it would be accessible between functions. Am I wrong to assume this? If not, what is wrong with this code? It seems the $db resource is the problem.
Thank you for taking a look, and thank you for any assistance!


Sign In
Create Account


Back to top









