Author | Message | Time |
---|---|---|
FrOzeN | How can I setup my MySQL_Wrapper class to have global access? Eg: index.php: [code]include 'mysql.class.php'; include 'login.class.php'; $clsMySQL = &New MySQL_Wrapper; $clsLogin = &New LoginClass; $clsLogin->something();[/code] login.class.php: [code]class LoginClass { var $username; var $example; function something() { $sql_result = $clsMySQL->sql_query("SOMETHING ..."); return $sql_result; } }[/code] mysql.class.php: [code]class MySQL_Wrapper { var $sql_connection; function sql_query($query) { $str_result = mysql_query($query, $this->sql_connection); return $str_result; } }[/code] | November 27, 2006, 4:43 AM |
Ersan | You can either use global $clsMySQL; (often considered bad programming, depends on how many of your classes will need to access the object), or pass it as a reference variable to the classes you're using (preferably through the constructor), like so: index.php [code] $clsLogin = New LoginClass($clsMySQL); [/code] login.class.php: [code] class LoginClass { var $mysql; function LoginClass(&$mysql) { $this->mysql = &$mysql; } } [/code] You really shouldn't be using &New to assign your mysql and login classes as reference variables either (I don't even think you're supposed to be able to). Either way, you won't be able to call them from loaded classes that way. | November 27, 2006, 5:17 AM |
FrOzeN | Thanks, that clears it up. I'll parse it through in the class initialization as you showed. | November 27, 2006, 5:42 AM |
Quarantine | I wonder if you could use the Singleton Design Pattern in PHP hmm.. | November 27, 2006, 6:57 PM |
Ersan | Not the same way you can in other languages, I really hate 'design patterns'. Write what pertains to your program, globalizing anything in php wastes heaps of memory and is generally inefficient, if your class doesn't need access to the object, then don't waste memory giving it access. As far as Singleton goes, the only way to accomplish it in PHP is to either write a function to create the object inside every class, or create an array of object variable references that you keep track of. Either way you're globally referencing the object. | November 27, 2006, 9:42 PM |
FrOzeN | I got the class working but another problem has arose from this. [quote]Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /home2/frozen/public_html/myprose/mysql.class.php on line 42[/quote] Before the code in test.php was: [code]<?php session_start(); include 'mysql.class.php'; include 'login.class.php'; $clsMySQL = New MySQL_Wrapper; $clsLogin = New LoginClass($clsMySQL); $clsMySQL->db_host = "localhost"; $clsMySQL->db_username = "<snipped>"; $clsMySQL->db_password = "<snipped>"; $clsMySQL->db_database = "<snipped>"; if ($clsMySQL->sql_connect()) echo "Connected to MySQL!<br />\n"; if ($clsMySQL->select_db()) echo "Connected to MyProse Database!<br />\n"; // ip check: $your_ip = $_SERVER['REMOTE_ADDR']; if ($clsMySQL->sql_query("SELECT * FROM ipaddresses WHERE ip='$your_ip'")) { if (mysql_num_rows($clsMySQL->sql_result)) { echo "Your IP Address exists in our database."; } else { echo "Your IP Address doesn't exist in our database."; } } ?>[/code] That code still works fine. Then I changed the bottom part to: [code]<?php /* ... snipped ... */ // ip check: $clsLogin->check_ip(); ?>[/code]And then added the function check_ip() to the LoginClass: [code]<?php /* ... snipped ... */ function check_ip() { $your_ip = $_SERVER['REMOTE_ADDR']; if ($this->objMySQL->sql_query("SELECT * FROM ipaddresses WHERE ip='$your_ip'")) { if (mysql_num_rows($this->objMySQL->sql_result)) { echo "Your IP Address exists in our database."; } else { echo "Your IP Address doesn't exist in our database."; } } } /* ... snipped ... */ ?> [/code] And that now gives me that error which I quoted at the start of my post. Also, for clarification my LoginClass is setup like so: [code]<?php class LoginClass{ var $objMySQL; function LoginClass($mysql) { $this->objMySQL = &$mysql; } // etc.. ?>[/code] Any idea why the mysql_ functions aren't working? [EDIT] Just googled a bit and it says that it's caused due to the MySQL connection not establishing correctly. Why would that be if the MySQL establishes fine and runs queries when called from test.php?, yet not work when called from inside the LoginClass? | November 28, 2006, 10:28 AM |
rabbit | Connect to the sever and select the database before you reference the wrapper to anything else. | November 28, 2006, 12:02 PM |
Ersan | function LoginClass($mysql) { that needs to be a reference var (like i told you the first time...): function LoginClass(&$mysql) { Should fix it. | November 28, 2006, 9:01 PM |
FrOzeN | [quote author=Ersan link=topic=16078.msg161920#msg161920 date=1164747664] function LoginClass($mysql) { that needs to be a reference var (like i told you the first time...): function LoginClass(&$mysql) { Should fix it. [/quote]Oops, didn't read your code closely enough. Works now. :) | November 29, 2006, 4:18 AM |