SharkBait Posted June 15, 2007 Share Posted June 15, 2007 Ok I'm new to this whole creating classes thing so I stared simple with a MySQL connector class: <?php class MySQL { var $link; var $query; var $results = array(); function Connect($DBHOST, $DBUSER, $DBPASS, $DBNAME) { // Connect to MYSQL DB with defined settings from config.php $this->link = mysql_connect($DBHOST, $DBUSER, $DBPASS) or die("MySQL Cannot Connect: ". mysql_error()); mysql_select_db($DBNAME, $this->link) or die("MySQL Cannot Select {DBNAME): <br />". mysql_error()); return $this->link; } } ?> But I am having troubles passing the database connection arguments and it is telling me its missing it. This is how I do it: <?php $MySQL = new MySQL(); $MyLink = $MySQL->Connect("192.168.1.1", "user", "pass", "myTable"); ?> What am i doing wrong? Link to comment https://forums.phpfreaks.com/topic/55730-passing-arguments-missing-arguments/ Share on other sites More sharing options...
per1os Posted June 15, 2007 Share Posted June 15, 2007 Can you post the exact error? Link to comment https://forums.phpfreaks.com/topic/55730-passing-arguments-missing-arguments/#findComment-275466 Share on other sites More sharing options...
SharkBait Posted June 15, 2007 Author Share Posted June 15, 2007 Warning: Missing argument 1 for MySQL::Connect(), called in /home/tingram/public_html/feedback/includes/header.html on line 20 and defined in /home/tingram/public_html/feedback/includes/c-mysql.php on line 21 Warning: Missing argument 2 for MySQL::Connect(), called in /home/tingram/public_html/feedback/includes/header.html on line 20 and defined in /home/tingram/public_html/feedback/includes/c-mysql.php on line 21 Warning: Missing argument 3 for MySQL::Connect(), called in /home/tingram/public_html/feedback/includes/header.html on line 20 and defined in /home/tingram/public_html/feedback/includes/c-mysql.php on line 21 Warning: Missing argument 4 for MySQL::Connect(), called in /home/tingram/public_html/feedback/includes/header.html on line 20 and defined in /home/tingram/public_html/feedback/includes/c-mysql.php on line 21 Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'www-data'@'localhost' (using password: NO) in /home/tingram/public_html/feedback/includes/c-mysql.php on line 23 MySQL Cannot Connect: Access denied for user 'www-data'@'localhost' (using password: NO) Link to comment https://forums.phpfreaks.com/topic/55730-passing-arguments-missing-arguments/#findComment-275471 Share on other sites More sharing options...
per1os Posted June 15, 2007 Share Posted June 15, 2007 Can you post the code where it is actually being used? Link to comment https://forums.phpfreaks.com/topic/55730-passing-arguments-missing-arguments/#findComment-275509 Share on other sites More sharing options...
SharkBait Posted June 15, 2007 Author Share Posted June 15, 2007 c-myslq.php <?php if(!defined("DBNAME")) { echo "<p>I cannot locate my database variables!</p>"; exit(); } class MySQL { var $link; var $query; // Database variables var $host; var $username; var $password; var $database; var $results = array(); function Connect($host, $username, $password, $database) { // Connect to MYSQL DB with defined settings from config.php $this->link = mysql_connect($this->host, $this->username, $this->password) or die("MySQL Cannot Connect: ". mysql_error()); // Select database to work with mysql_select_db($this->database, $this->link) or die("MySQL Cannot Select {DBNAME): <br />". mysql_error()); // Return the link to be used later return $this->link; } function Disconnect($link) { mysql_close($link); } function DoQuery($query) { // Basic Query Execution $this->query = mysql_query($query) or die("MySQL DoQuery Error: <br />{$query} <br />". mysql_error()); return $this->query; } function FetchArray($query) { // Retrieve results of DoQuery $this->results = mysql_fetch_array($query, MYSQL_ASSOC); return $this->results; } } ?> search.php <?php require("c-mysql.php"); // UPDATE SugarCRM bu INSERTING a note about tracking numbers // Find out ID for Customer in Sugar // Create new DEFINES for secondary MySQL connection $DBNAME = "sugarcrm"; $DBHOST = "192.168.1.1"; $DBUSER = "userblah"; $DBPASS = "blah"; $MySQLSugar = new MySQL(); $SugarLink = $MySQLSugar->Connect($DBHOST, $DBUSER, $DBPASS, $DBNAME); $strqry = "SELECT id FROM sugarcrm.accounts WHERE name = '{$company}'"; $query = $MySQLSugar->DoQuery($strqry); $companyInfo = $MySQLSugar->FetchArray($query); ?> Link to comment https://forums.phpfreaks.com/topic/55730-passing-arguments-missing-arguments/#findComment-275521 Share on other sites More sharing options...
per1os Posted June 16, 2007 Share Posted June 16, 2007 <?php function Connect($host, $username, $password, $database) { $this->host = $host; $this->username = $username; $this->password = $password; // Connect to MYSQL DB with defined settings from config.php $this->link = mysql_connect($this->host, $this->username, $this->password) or die("MySQL Cannot Connect: ". mysql_error()); You have to define the $this->var before you can really "use" properly, as the script does not know you want $host throughout the whole class, try that. Link to comment https://forums.phpfreaks.com/topic/55730-passing-arguments-missing-arguments/#findComment-275625 Share on other sites More sharing options...
emehrkay Posted June 16, 2007 Share Posted June 16, 2007 my advice, to extend on what frost is saying, is to put that declaration in the constructor class MySQL{ var $user .... function MySQL($user...){ $this->user = $user ... } function connect(){ $this->link = mysql_connect($this->host, $this->username, $this->password) or die("MySQL Cannot Connect: ". mysql_error()); ... } } and youd use it like this $x = new MySQL('user'...); $x->connect(); or you could even put the connect method in the constructor so that it automatically makes the connection when you instantiate the object //inside MySQL class function MySQL($user...){ $this->user = $user... $this->connect(); } then all you have to do is $x = new MySQL('user'...); where ... means you fill in the rest Link to comment https://forums.phpfreaks.com/topic/55730-passing-arguments-missing-arguments/#findComment-275643 Share on other sites More sharing options...
SharkBait Posted June 17, 2007 Author Share Posted June 17, 2007 Ah ok that makes sense! Thanks for the replies! Link to comment https://forums.phpfreaks.com/topic/55730-passing-arguments-missing-arguments/#findComment-276119 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.