[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 352
  • Last Modified:

Having trouble with my php varaible!!

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

0
FairyBusiness
Asked:
FairyBusiness
  • 11
  • 9
1 Solution
 
Mohamed AbowardaSoftware EngineerCommented:
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.
0
 
FairyBusinessAuthor 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

0
 
Mohamed AbowardaSoftware EngineerCommented:
> 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.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
FairyBusinessAuthor 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.
0
 
FairyBusinessAuthor 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

0
 
Mohamed AbowardaSoftware EngineerCommented:
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

0
 
FairyBusinessAuthor 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?!
0
 
Mohamed AbowardaSoftware EngineerCommented:
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

0
 
FairyBusinessAuthor Commented:
why would I rename my variable?? what does that do?
0
 
FairyBusinessAuthor 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.
0
 
FairyBusinessAuthor 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

0
 
Mohamed AbowardaSoftware EngineerCommented:
"$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

0
 
FairyBusinessAuthor Commented:
What errors did you fix??

anyways, its still messing up  http://auroriella.com/gallery.php
0
 
Mohamed AbowardaSoftware EngineerCommented:
The database class should work with the last code I posted without any problem.

What's "/gallery.php" source code?
0
 
FairyBusinessAuthor 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

0
 
Mohamed AbowardaSoftware EngineerCommented:
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

0
 
FairyBusinessAuthor 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??
0
 
Mohamed AbowardaSoftware EngineerCommented:
You don't have to close the connection, it will terminate the connection automatically after the page finish executing, however you can terminate the connection anytime after you finish all your database related code.

You can use your original code at your question, and just remove the line $database->close_conn(); at the end of the code and it will work too!

Basically, the function close_conn() was clearing the connection resource, as you see the line
unset($this->conn); inside the function close_conn() it's CLEARING the variable so it become not a MySQL connection resource.

Additionally, it close the database connection, you won't have access to the database while there is no database connection.
0
 
FairyBusinessAuthor Commented:
Thanks again :))
0
 
Mohamed AbowardaSoftware EngineerCommented:
You are welcome again :)
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 11
  • 9
Tackle projects and never again get stuck behind a technical roadblock.
Join Now