We help IT Professionals succeed at work.

Having trouble with my php varaible!!

FairyBusiness
on
Medium Priority
367 Views
Last Modified: 2012-06-27
Hi, I keep getting this error message:

Notice: Undefined property: MySQLDatabase::$conn in /hermes/web09c/b2950/moo.auroriellacom/includes/database.php on line 26

Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /hermes/web09c/b2950/moo.auroriellacom/includes/database.php on line 26
Database query failed:

but I can't figure out what's wrong with my line 26!  Does anyone else see something I missed??
<?php
require_once 'includes/constants.php';

class MySQLDatabase { 
// Declares the variable
private $conn;
// Automatically opens the connection
function __construct() {
	$this->open_conn();
}
// Opens the connection
public function open_conn() {
	$this->conn = mysql_connect(SERVER, USER, PASSWORD);
	if(!$this->conn) {
		die("Database connection failed: " . mysql_error());
	}
	else {
	$db = mysql_select_db(NAME, $this->conn);
	if (!$db) {
		die("Database selection failed: " . mysql_error());
	}
  }
}
// Checks the sql result
public function query($sql) {
	$result = mysql_query($sql, $this->conn);
	$this->confirm_query($result);
	return $result;
}
// Checks the query
private function confirm_query($result) {
	if(!$result) {
		die("Database query failed: " . mysql_error());
	}
}
// Checks to see if magic quotes are turned on
// Checks to see if the user's version of php is 4.3.0 or higher
// If the user has magic quotes turned on & php 4.3.0 or higher then use the mysql_real_escape_string function
// If not then addslashes to the value
public function mysql_clean_strings($value) {
	$magic_quotes_active = get_magic_quotes_gpc();
	$new_enough_php = function_exists("mysql_real_escape_string"); // PHP >= 4.30
	if($new_enough_php) { // PHP 4.3.0 or higher
		// undo any magic quote effects so mysql_real_escape_string can do the work
		if($magic_quotes_active) {
			$value = stripslashes($value);
			$value = mysql_real_escape_string($value);
		}
	}
	else { // before PHP 4.3.0
		// if magic quotes aren't already on then add slashes manually
		if(!magic_quotes_active) {
			$value = addslashes($value);
			// if magic quotes are active, then the slashes already exist
		}
	}
	return $value;
}
// Closes the connection
public function close_conn() {
	if(isset($this->conn)) {
		mysql_close($this->conn);
		unset($this->conn);
	}
  } 
} // End class

$database = new MySQLDatabase();
$database->close_conn();
?>

Open in new window

Comment
Watch Question

Mohamed AbowardaSenior Software Engineer
CERTIFIED EXPERT

Commented:
I found that your script is working after defining the database connection variables (SERVER, USER, PASSWORD), upload 'includes/constants.php' so I can check both scripts.

Author

Commented:
Ok, but I'm not showing my password :)

<?php
defined('SERVER') ? null : define("SERVER", "auroriellacom.fatcowmysql.com");
defined('USER') ? null : define("USER", "tanyaleithoff");
defined('PASSWORD') ? null : define("PASSWORD", "*****");
defined('NAME') ? null : define("NAME", "auroriella");
?>

Open in new window

Mohamed AbowardaSenior Software Engineer
CERTIFIED EXPERT

Commented:
> Ok, but I'm not showing my password :)
You always like to hide the passwords :P

Well, I see the scripts are working perfectly on localhost, perhaps you are using old PHP version?
Try to change the way you declare the variable $conn in, try using global.

change "private $cnn;" to "global $cnn;"
Add "global $cnn;" at the beginning of each function that will access the variable $cnn.

Author

Commented:
Well, shouldn't I hide the password? lol

so declare:

global private $conn?

I am working off of a tutorial online. . . so I am taking their lead. But its from 2009.

Author

Commented:
Ok, I did this but its still didnt work:

Parse error: syntax error, unexpected T_GLOBAL, expecting T_FUNCTION in /hermes/web09c/b2950/moo.auroriellacom/includes/database.php on line 6
<?php
require_once 'includes/constants.php';

class MySQLDatabase { 
// Declares the variable
global $conn;
// Automatically opens the connection
function __construct() {
	global $conn;
	$this->open_conn();
}
// Opens the connection
public function open_conn() {
	global $conn;
	$this->conn = mysql_connect(SERVER, USER, PASSWORD);
	if(!$this->conn) {
		die("Database connection failed: " . mysql_error());
	}
	else {
	$db = mysql_select_db(NAME, $this->conn);
	if (!$db) {
		die("Database selection failed: " . mysql_error());
	}
  }
}
// Checks the sql result
public function query($sql) {
	global $conn;
	$result = mysql_query($sql, $this->conn);
	$this->confirm_query($result);
	return $result;
}
// Checks the query
private function confirm_query($result) {
	if(!$result) {
		die("Database query failed: " . mysql_error());
	}
}
// Checks to see if magic quotes are turned on
// Checks to see if the user's version of php is 4.3.0 or higher
// If the user has magic quotes turned on & php 4.3.0 or higher then use the mysql_real_escape_string function
// If not then addslashes to the value
public function mysql_clean_strings($value) {
	$magic_quotes_active = get_magic_quotes_gpc();
	$new_enough_php = function_exists("mysql_real_escape_string"); // PHP >= 4.30
	if($new_enough_php) { // PHP 4.3.0 or higher
		// undo any magic quote effects so mysql_real_escape_string can do the work
		if($magic_quotes_active) {
			$value = stripslashes($value);
			$value = mysql_real_escape_string($value);
		}
	}
	else { // before PHP 4.3.0
		// if magic quotes aren't already on then add slashes manually
		if(!magic_quotes_active) {
			$value = addslashes($value);
			// if magic quotes are active, then the slashes already exist
		}
	}
	return $value;
}
// Closes the connection
public function close_conn() {
	global $conn;
	if(isset($this->conn)) {
		mysql_close($this->conn);
		unset($this->conn);
	}
  } 
} // End class

$database = new MySQLDatabase();
$database->close_conn();
?>

Open in new window

Mohamed AbowardaSenior Software Engineer
CERTIFIED EXPERT

Commented:
Declare "global $conn;" outside the class.

The following code should work:
<?php
require_once 'includes/constants.php';

// Declares the variable
global $conn;

class MySQLDatabase { 
// Automatically opens the connection
function __construct() {
	global $conn;
	$this->open_conn();
}
// Opens the connection
public function open_conn() {
	global $conn;
	$this->conn = mysql_connect(SERVER, USER, PASSWORD);
	if(!$this->conn) {
		die("Database connection failed: " . mysql_error());
	}
	else {
	$db = mysql_select_db(NAME, $this->conn);
	if (!$db) {
		die("Database selection failed: " . mysql_error());
	}
  }
}
// Checks the sql result
public function query($sql) {
	global $conn;
	$result = mysql_query($sql, $this->conn);
	$this->confirm_query($result);
	return $result;
}
// Checks the query
private function confirm_query($result) {
	if(!$result) {
		die("Database query failed: " . mysql_error());
	}
}
// Checks to see if magic quotes are turned on
// Checks to see if the user's version of php is 4.3.0 or higher
// If the user has magic quotes turned on & php 4.3.0 or higher then use the mysql_real_escape_string function
// If not then addslashes to the value
public function mysql_clean_strings($value) {
	$magic_quotes_active = get_magic_quotes_gpc();
	$new_enough_php = function_exists("mysql_real_escape_string"); // PHP >= 4.30
	if($new_enough_php) { // PHP 4.3.0 or higher
		// undo any magic quote effects so mysql_real_escape_string can do the work
		if($magic_quotes_active) {
			$value = stripslashes($value);
			$value = mysql_real_escape_string($value);
		}
	}
	else { // before PHP 4.3.0
		// if magic quotes aren't already on then add slashes manually
		if(!magic_quotes_active) {
			$value = addslashes($value);
			// if magic quotes are active, then the slashes already exist
		}
	}
	return $value;
}
// Closes the connection
public function close_conn() {
	global $conn;
	if(isset($this->conn)) {
		mysql_close($this->conn);
		unset($this->conn);
	}
  } 
} // End class

$database = new MySQLDatabase();
$database->close_conn();
?>

Open in new window

Author

Commented:
Nope, I copied your code exactly and I'm back to my original errors:

Notice: Undefined property: MySQLDatabase::$conn in /hermes/web09c/b2950/moo.auroriellacom/includes/database.php on line 30

Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /hermes/web09c/b2950/moo.auroriellacom/includes/database.php on line 30
Database query failed:

Even if your code had worked, I mean whats the point to classes if I have to go through all that extra work to make them work?  Classes are the one thing I'm still REALLY learning in php, and basically my last thing to get. . . I'm having a hard time figuring out why to use them instead of just having a library calling functions from?!
Mohamed AbowardaSenior Software Engineer
CERTIFIED EXPERT

Commented:
I forgot to say:

You need to change "$this->conn" to "$cnn".

Change:
$result = mysql_query($sql, $this->conn);

Open in new window


To:
$result = mysql_query($sql, $cnn);

Open in new window

Author

Commented:
why would I rename my variable?? what does that do?

Author

Commented:
also if I'm not going to use the $this->  then why am I even bother with classes. ..  this just seems silly. I'm just taking functions from my library and sticking them into a class basically.

Author

Commented:
I did this and got this error message: (its only one error this time though)

Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /hermes/web09c/b2950/moo.auroriellacom/includes/database.php on line 30
Database query failed:
<?php
require_once 'includes/constants.php';

// Declares the variable
global $conn;

class MySQLDatabase { 
// Automatically opens the connection
function __construct() {
	global $conn;
	$this->open_conn();
}
// Opens the connection
public function open_conn() {
	global $conn;
	$this->conn = mysql_connect(SERVER, USER, PASSWORD);
	if(!$this->conn) {
		die("Database connection failed: " . mysql_error());
	}
	else {
	$db = mysql_select_db(NAME, $this->conn);
	if (!$db) {
		die("Database selection failed: " . mysql_error());
	}
  }
}
// Checks the sql result
public function query($sql) {
	global $conn;
	$result = mysql_query($sql, $conn);
	$this->confirm_query($result);
	return $result;
}
// Checks the query
private function confirm_query($result) {
	if(!$result) {
		die("Database query failed: " . mysql_error());
	}
}
// Checks to see if magic quotes are turned on
// Checks to see if the user's version of php is 4.3.0 or higher
// If the user has magic quotes turned on & php 4.3.0 or higher then use the mysql_real_escape_string function
// If not then addslashes to the value
public function mysql_clean_strings($value) {
	$magic_quotes_active = get_magic_quotes_gpc();
	$new_enough_php = function_exists("mysql_real_escape_string"); // PHP >= 4.30
	if($new_enough_php) { // PHP 4.3.0 or higher
		// undo any magic quote effects so mysql_real_escape_string can do the work
		if($magic_quotes_active) {
			$value = stripslashes($value);
			$value = mysql_real_escape_string($value);
		}
	}
	else { // before PHP 4.3.0
		// if magic quotes aren't already on then add slashes manually
		if(!magic_quotes_active) {
			$value = addslashes($value);
			// if magic quotes are active, then the slashes already exist
		}
	}
	return $value;
}
// Closes the connection
public function close_conn() {
	global $conn;
	if(isset($this->conn)) {
		mysql_close($this->conn);
		unset($this->conn);
	}
  } 
} // End class

$database = new MySQLDatabase();
$database->close_conn();
?>

Open in new window

Mohamed AbowardaSenior Software Engineer
CERTIFIED EXPERT

Commented:
"$this->conn" access the variable as it's declared INSIDE the class, while using $cnn directly access the variable as LOCAL variable, when you add the line "global $cnn;" inside the function it retrieve the value from the script main variable declaration at the top (line 5).

You have some errors with your code, I fixed them for you, here is the final code:
<?php
require_once 'includes/constants.php';

// Declares the variable
global $conn;

class MySQLDatabase { 
// Automatically opens the connection
function __construct() {
	$this->open_conn();
}
// Opens the connection
public function open_conn() {
	global $conn;
	$conn = mysql_connect(SERVER, USER, PASSWORD);
	if(!$conn) {
		die("Database connection failed: " . mysql_error());
	}
	else {
	$db = mysql_select_db(NAME, $conn);
	if (!$db) {
		die("Database selection failed: " . mysql_error());
	}
  }
}
// Checks the sql result
public function query($sql) {
	global $conn;
	$result = mysql_query($sql, $conn);
	$this->confirm_query($result);
	return $result;
}
// Checks the query
private function confirm_query($result) {
	if(!$result) {
		die("Database query failed: " . mysql_error());
	}
}
// Checks to see if magic quotes are turned on
// Checks to see if the user's version of php is 4.3.0 or higher
// If the user has magic quotes turned on & php 4.3.0 or higher then use the mysql_real_escape_string function
// If not then addslashes to the value
public function mysql_clean_strings($value) {
	$magic_quotes_active = get_magic_quotes_gpc();
	$new_enough_php = function_exists("mysql_real_escape_string"); // PHP >= 4.30
	if($new_enough_php) { // PHP 4.3.0 or higher
		// undo any magic quote effects so mysql_real_escape_string can do the work
		if($magic_quotes_active) {
			$value = stripslashes($value);
			$value = mysql_real_escape_string($value);
		}
	}
	else { // before PHP 4.3.0
		// if magic quotes aren't already on then add slashes manually
		if(!magic_quotes_active) {
			$value = addslashes($value);
			// if magic quotes are active, then the slashes already exist
		}
	}
	return $value;
}
// Closes the connection
public function close_conn() {
	global $conn;
	if(isset($conn)) {
		mysql_close($conn);
		unset($conn);
	}
  } 
} // End class

$database = new MySQLDatabase();
$database->close_conn();
?>

Open in new window

Author

Commented:
What errors did you fix??

anyways, its still messing up  http://auroriella.com/gallery.php
Mohamed AbowardaSenior Software Engineer
CERTIFIED EXPERT

Commented:
The database class should work with the last code I posted without any problem.

What's "/gallery.php" source code?

Author

Commented:
Its some testing I was doing.

<?php
ini_set('display_errors' ,1); 
error_reporting(E_ALL);

require_once 'includes/database.php';

if(isset($database)) {
	echo "true";
}
else {
	echo "false";
}

echo "Is this working?";

echo $database->mysql_clean_strings("It's working?<br />");

$sql = "INSERT INTO users (id, username) ";
$sql .= "VALUES (1, 'lala')";
$result = $database->query($sql);

$sql = "SELECT * FROM users WHERE id=1";
$result = $database->query($sql);
$found_user = mysql_fetch_array($result);
echo $found_user['username'];

?>

Open in new window

Mohamed AbowardaSenior Software Engineer
CERTIFIED EXPERT

Commented:
At the end of the script "database.php" you close the database connection by using the method "$database->close_conn();" and then you try to use while the connection is not open at "gallery.php".

Remove the line $database->close_conn(); "database.php" should be as the following:
<?php
require_once 'includes/constants.php';

// Declares the variable
global $conn;

class MySQLDatabase { 
// Automatically opens the connection
function __construct() {
	$this->open_conn();
}
// Opens the connection
public function open_conn() {
	global $conn;
	$conn = mysql_connect(SERVER, USER, PASSWORD);
	if(!$conn) {
		die("Database connection failed: " . mysql_error());
	}
	else {
	$db = mysql_select_db(NAME, $conn);
	if (!$db) {
		die("Database selection failed: " . mysql_error());
	}
  }
}
// Checks the sql result
public function query($sql) {
	global $conn;
	$result = mysql_query($sql, $conn);
	$this->confirm_query($result);
	return $result;
}
// Checks the query
private function confirm_query($result) {
	if(!$result) {
		die("Database query failed: " . mysql_error());
	}
}
// Checks to see if magic quotes are turned on
// Checks to see if the user's version of php is 4.3.0 or higher
// If the user has magic quotes turned on & php 4.3.0 or higher then use the mysql_real_escape_string function
// If not then addslashes to the value
public function mysql_clean_strings($value) {
	$magic_quotes_active = get_magic_quotes_gpc();
	$new_enough_php = function_exists("mysql_real_escape_string"); // PHP >= 4.30
	if($new_enough_php) { // PHP 4.3.0 or higher
		// undo any magic quote effects so mysql_real_escape_string can do the work
		if($magic_quotes_active) {
			$value = stripslashes($value);
			$value = mysql_real_escape_string($value);
		}
	}
	else { // before PHP 4.3.0
		// if magic quotes aren't already on then add slashes manually
		if(!magic_quotes_active) {
			$value = addslashes($value);
			// if magic quotes are active, then the slashes already exist
		}
	}
	return $value;
}
// Closes the connection
public function close_conn() {
	global $conn;
	if(isset($conn)) {
		mysql_close($conn);
		unset($conn);
	}
  } 
} // End class

$database = new MySQLDatabase();
?>

Open in new window

Author

Commented:
It works now!! You know I think that may have been the problem the whole time.  

I wonder if I even needed to making all those things global and change the variables.  That is where they had the close in the tutorial. . . so should I put the close at the end of my gallery page??
Senior Software Engineer
CERTIFIED EXPERT
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
Thanks again :))
Mohamed AbowardaSenior Software Engineer
CERTIFIED EXPERT

Commented:
You are welcome again :)
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.