FairyBusiness
asked on
Having trouble with my php varaible!!
Hi, I keep getting this error message:
Notice: Undefined property: MySQLDatabase::$conn in /hermes/web09c/b2950/moo.a uroriellac om/include s/database .php on line 26
Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /hermes/web09c/b2950/moo.a uroriellac om/include s/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??
Notice: Undefined property: MySQLDatabase::$conn in /hermes/web09c/b2950/moo.a
Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /hermes/web09c/b2950/moo.a
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();
?>
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.
ASKER
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");
?>
> 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.
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.
ASKER
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.
so declare:
global private $conn?
I am working off of a tutorial online. . . so I am taking their lead. But its from 2009.
ASKER
Ok, I did this but its still didnt work:
Parse error: syntax error, unexpected T_GLOBAL, expecting T_FUNCTION in /hermes/web09c/b2950/moo.a uroriellac om/include s/database .php on line 6
Parse error: syntax error, unexpected T_GLOBAL, expecting T_FUNCTION in /hermes/web09c/b2950/moo.a
<?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();
?>
Declare "global $conn;" outside the class.
The following code should work:
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();
?>
ASKER
Nope, I copied your code exactly and I'm back to my original errors:
Notice: Undefined property: MySQLDatabase::$conn in /hermes/web09c/b2950/moo.a uroriellac om/include s/database .php on line 30
Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /hermes/web09c/b2950/moo.a uroriellac om/include s/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?!
Notice: Undefined property: MySQLDatabase::$conn in /hermes/web09c/b2950/moo.a
Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /hermes/web09c/b2950/moo.a
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?!
I forgot to say:
You need to change "$this->conn" to "$cnn".
Change:
To:
You need to change "$this->conn" to "$cnn".
Change:
$result = mysql_query($sql, $this->conn);
To:
$result = mysql_query($sql, $cnn);
ASKER
why would I rename my variable?? what does that do?
ASKER
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.
ASKER
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.a uroriellac om/include s/database .php on line 30
Database query failed:
Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /hermes/web09c/b2950/moo.a
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();
?>
"$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:
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();
?>
ASKER
The database class should work with the last code I posted without any problem.
What's "/gallery.php" source code?
What's "/gallery.php" source code?
ASKER
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'];
?>
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:
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();
?>
ASKER
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??
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??
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks again :))
You are welcome again :)