Jump to content

Resource handle not working in class function body

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
jackolantern

jackolantern

    Learning Programmer

  • Members
  • PipPipPip
  • 47 posts
I am just starting to experiment a bit with the object oriented aspects of PHP. My first small project was to make a database connection class for handling input and output to MySQL. However, I am having a problem with it that may be due to function scope. I am not sure how best to handle the issue if that is the problem. Here is my database connection class file:

<?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

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!

#2
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
try $this->db instead, so you say it's the objects $db you mean. I need to do that at least.

$showResult = $database->ShowDBresource();
As you throw exceptions here in you code, you need to "try" that line.


try {

    $showResult = $database->ShowDBresource();

} catch (Exception e) {

  // whatever you wanna do

}

__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#3
jackolantern

jackolantern

    Learning Programmer

  • Members
  • PipPipPip
  • 47 posts
Woo hoo! That was it. Thank you! I should have tried using $this->db to begin with, but my programming background is .NET and Java, and both of those don't require this.variable provided there is no ambiguity. And as far as the exceptions, they would definitely be handled in production code, and now they are not firing that the initial issue has been fixed.

Thanks again! :)