waynew Posted October 2, 2008 Share Posted October 2, 2008 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 https://forums.phpfreaks.com/topic/126813-mysql_real_escape_string-expects-parameter-2-to-be-resource/ Share on other sites More sharing options...
DarkWater Posted October 2, 2008 Share Posted October 2, 2008 Can I see the instantiation of the Database class that you use? Link to comment https://forums.phpfreaks.com/topic/126813-mysql_real_escape_string-expects-parameter-2-to-be-resource/#findComment-655929 Share on other sites More sharing options...
waynew Posted October 2, 2008 Author Share Posted October 2, 2008 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 https://forums.phpfreaks.com/topic/126813-mysql_real_escape_string-expects-parameter-2-to-be-resource/#findComment-655935 Share on other sites More sharing options...
DarkWater Posted October 2, 2008 Share Posted October 2, 2008 You're instantiating a new Database in User, instead of passing in the existing one, and the new one isn't connected. Also, why in the world are you using PHP4 OOP syntax? It's really not good to be using any more. Link to comment https://forums.phpfreaks.com/topic/126813-mysql_real_escape_string-expects-parameter-2-to-be-resource/#findComment-655936 Share on other sites More sharing options...
waynew Posted October 2, 2008 Author Share Posted October 2, 2008 Thanks for the help but I've just realised that I've made a pretty retarded mistake! Link to comment https://forums.phpfreaks.com/topic/126813-mysql_real_escape_string-expects-parameter-2-to-be-resource/#findComment-655937 Share on other sites More sharing options...
revraz Posted October 2, 2008 Share Posted October 2, 2008 If you only have one connection open at a time, just remove it. Link to comment https://forums.phpfreaks.com/topic/126813-mysql_real_escape_string-expects-parameter-2-to-be-resource/#findComment-655938 Share on other sites More sharing options...
waynew Posted October 2, 2008 Author Share Posted October 2, 2008 Yea I noticed that. Wasn't thinking right at all. Sorry, I'm used to Java so I always have a habit of creating my constructors as the class name. Doesn't feel right to me otherwise. Link to comment https://forums.phpfreaks.com/topic/126813-mysql_real_escape_string-expects-parameter-2-to-be-resource/#findComment-655941 Share on other sites More sharing options...
dennismonsewicz Posted October 2, 2008 Share Posted October 2, 2008 WOW java? I feel your pain man! Link to comment https://forums.phpfreaks.com/topic/126813-mysql_real_escape_string-expects-parameter-2-to-be-resource/#findComment-655943 Share on other sites More sharing options...
discomatt Posted October 2, 2008 Share Posted October 2, 2008 WOW java? I feel your pain man! Huh? PHP5 took a lot of OOP standards from java Link to comment https://forums.phpfreaks.com/topic/126813-mysql_real_escape_string-expects-parameter-2-to-be-resource/#findComment-655996 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.