Jump to content

mysql_real_escape_string() expects parameter 2 to be resource


waynew

Recommended Posts

Hey guys. I'm creating my own custom db class and I've come across a slight problem. Firstly, let me show you the class.

 

<?php

class Database{

	var $password = "";
	var $username = "root";
	var $hostname = "localhost:8080";
	var $database = "";
	var $last_result = NULL;
	var $con;

	function connect(){
		$connection = mysql_connect($this->hostname,$this->username,$this->password) or die(mysql_error());			
		$this->con = $connection;
		if($this->database != ""){
			mysql_query("USE ".$this->database) or die(mysql_error());
		}
	}

	function query($query){
		$result = mysql_query($query,$this->con) or die(mysql_error());
		$this->last_result = $result;
		return $result;
	}

	function set_database($db){
		$this->query("USE $db");
		$this->database = $db;
	}

	function clean($string){
		if(get_magic_quotes_gpc() == 1){
			$string = stripslashes($string);
		}
		$string = mysql_real_escape_string($string,$this->con);
		return $string;
	}

	function free($result){
		mysql_free_result($result);
	}
}
?>

Now I keep getting this whenever I use the clean function error:

 

mysql_real_escape_string() expects parameter 2 to be resource, null given

 

I know for a fact that I've successfully established a connection as the function query works fine.

 

What could be the problem?

 

 

Link to comment
Share on other sites

Here it is,

 

$user = new User();
$db = new Database();
$db->connect();
$db->set_database("classtest");
$user->register_user("Wayne","Tester","Email","Name");

 

The class user, which uses the clean function of the DB class goes like:

 

<?php

class User{

var $db;

function User(){
	$database = new Database();
	$this->db = $database;
}

function register_user($username,$password,$email,$name){
	$username = $this->db->clean($username);
	$password = $this->db->clean($password);
	$email = $this->db->clean($email);
	$name = $this->db->clean($name);
}
}

?>

 

I'm expecting something stupid. I've had long days these past few weeks.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.