Archadian Posted October 15, 2008 Share Posted October 15, 2008 On my homepage i am getting this: Warning: Missing argument 1 for Mysql::__construct(), called in F:\Inetpub\wwwroot\index.php on line 38 and defined in F:\Inetpub\wwwroot\include\class.php on line 18 Warning: Missing argument 2 for Mysql::__construct(), called in F:\Inetpub\wwwroot\index.php on line 38 and defined in F:\Inetpub\wwwroot\include\class.php on line 18 Warning: Missing argument 3 for Mysql::__construct(), called in F:\Inetpub\wwwroot\index.php on line 38 and defined in F:\Inetpub\wwwroot\include\class.php on line 18 Warning: Missing argument 4 for Mysql::__construct(), called in F:\Inetpub\wwwroot\index.php on line 38 and defined in F:\Inetpub\wwwroot\include\class.php on line 18 Warning: Missing argument 5 for Mysql::__construct(), called in F:\Inetpub\wwwroot\index.php on line 38 and defined in F:\Inetpub\wwwroot\include\class.php on line 18 Please check the Cabal connection. here is the class.php section that is causing it: class Mysql { private $host = 'localhost'; private $user = 'user'; private $pass = 'pass'; private $result = ''; private $db = ''; public function __construct($host, $user, $pass, $result, $db) { $this->host = $host; $this->user = $user; $this->pass = $pass; $this->result = $result; $this->db = $db; } public function cabal() { $filename = dirname(__FILE__); if (preg_match('/wwwroot/', $filename)) { $this->db = 'cabalweb'; } $conn = @mysql_connect($this->host, $this->user, $this->pass); $this->result = @mysql_select_db($this->db); if(!$conn || !$this->result) { die('Please check the Cabal connection.'); } return $this->result; } public function forums() { $filename = dirname(__FILE__); if (preg_match('/forums/', $filename)) { $this->db = 'c_forums'; } $conn = @mysql_connect($this->host, $this->user, $this->pass); $this->result = @mysql_select_db($this->db); if(!$conn || !$this->result) { die('Please check the Forums connection.'); } return $this->result; } } and here is the lines around line 38 on index.php: require('include/class.php'); $db =& new Mysql(); //line 38 $db->cabal(); It was working fine, I did not change anything and all of a sudden it stops working and gives me the warning messages. Any ideas on what this could be or what is wrong in the code? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/128494-it-was-working-now-it-isnt/ Share on other sites More sharing options...
Zane Posted October 15, 2008 Share Posted October 15, 2008 change this public function __construct($host, $user, $pass, $result, $db) { to this public function __construct($host='', $user='', $pass='', $result='', $db='') { Quote Link to comment https://forums.phpfreaks.com/topic/128494-it-was-working-now-it-isnt/#findComment-665954 Share on other sites More sharing options...
Archadian Posted October 15, 2008 Author Share Posted October 15, 2008 Thanks that worked but why wouldn't this work? private $host = 'localhost'; private $user = 'user'; private $pass = 'pass'; private $result = ''; private $db = ''; public function __construct($host, $user, $pass, $result, $db) { If $host = localhost, $user = username, $pass = password is being set why wouldn't it work for the __contruct()? Or is that just for the other variables in the other functions of the class? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/128494-it-was-working-now-it-isnt/#findComment-666061 Share on other sites More sharing options...
Acs Posted October 15, 2008 Share Posted October 15, 2008 That wouldn't work because those have nothing to do with the parameters names. Those are private properties that have the same names as the parameters in the __construct() Also a note from the manual: Do not use preg_match() if you only want to check if one string is contained in another string. Use strpos() or strstr() instead as they will be faster. And the & to have the reference is not necessary $db =& new Mysql() Quote Link to comment https://forums.phpfreaks.com/topic/128494-it-was-working-now-it-isnt/#findComment-666117 Share on other sites More sharing options...
keeB Posted October 15, 2008 Share Posted October 15, 2008 Your class should look like this (or similar): Notice how I broke up the commonality in to common methods, so that they can be reused and changed in a single place. <?php class Mysql { private $host = 'localhost'; private $user = 'user'; private $pass = 'pass'; private $result = ''; private $db = ''; public function __construct() { } public function set_db($name) { $filename = dirname(__FILE__); if (preg_match('/wwwroot/', $filename)) { $this->db = $name; } } public function get_db_connection() { $conn = @mysql_connect($this->host, $this->user, $this->pass); $this->result = @mysql_select_db($this->db); if(!$conn || !$this->result) { die('Please check the Cabal connection.'); } return $this->result; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/128494-it-was-working-now-it-isnt/#findComment-666249 Share on other sites More sharing options...
Acs Posted October 15, 2008 Share Posted October 15, 2008 Again the preg_match stuff.... Quote Link to comment https://forums.phpfreaks.com/topic/128494-it-was-working-now-it-isnt/#findComment-666500 Share on other sites More sharing options...
keeB Posted October 16, 2008 Share Posted October 16, 2008 I just refactored the structure, couldn't care less about the content as that's not the question at hand. Quote Link to comment https://forums.phpfreaks.com/topic/128494-it-was-working-now-it-isnt/#findComment-666704 Share on other sites More sharing options...
Acs Posted October 16, 2008 Share Posted October 16, 2008 If you know how to improve code you see you should! Quote Link to comment https://forums.phpfreaks.com/topic/128494-it-was-working-now-it-isnt/#findComment-666866 Share on other sites More sharing options...
keeB Posted October 16, 2008 Share Posted October 16, 2008 Then I would never get anything answered, as I'd be too busy getting things written 'properly' and arguing with everyone over semantics Quote Link to comment https://forums.phpfreaks.com/topic/128494-it-was-working-now-it-isnt/#findComment-667069 Share on other sites More sharing options...
Acs Posted October 16, 2008 Share Posted October 16, 2008 True Quote Link to comment https://forums.phpfreaks.com/topic/128494-it-was-working-now-it-isnt/#findComment-667072 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.