zarithe Posted March 24, 2007 Share Posted March 24, 2007 Hello everyone, I'm new to O.O.P but I've been doing php for a while now. Well anywho lets get to it.. I created a mysql class, which seems to work fine untill I try to call it from a function. When I try calling for a new db connection it either tells me I'm not use a password (which I am cause it works when its not being called from a function) or it just completely craps out on me. Anywho. My rambling doesnt help anything so lets get to the code... // Config.php <? $host = "localhost"; $mysql_user = "username"; $mysql_pass = "password"; $dbname = "dbname"; ?> I list the config simply because its required in the following code. // mysql.php <? require('config.php'); class mysql { function Connect($host, $mysql_user, $mysql_pass, $dbname) { $connection = mysql_connect("$host", "$mysql_user", "$mysql_pass") or die(mysql_error()); mysql_select_db("$dbname", $connection); } function Close() { mysql_close($this->connection); } function Query($sql) { $query = mysql_query($sql) or die(mysql_error()); return $query; } function FetchRow($query) { $rows = mysql_fetch_row($query); return $row; } function FetchArray($query) { $array = mysql_fetch_array($query); return $array; } function FetchNum($query) { $num = mysql_num_rows($query); return $num; } } ?> Ok so far there shouldnt be anything wrong with any of the code. Now we can get to the part I seem to be having issues with. // menus.php <? require('mysql.php'); function smenu() { $DB = new mysql(); $connection = $DB->connect($host, $mysql_user, $mysql_pass, $dbname); $sql = "SELECT * FROM server_menu WHERE parent_id = 0 ORDER BY id"; $query = $DB->Query($sql); while($array = $DB->FetchArray($query)) { extract($array); echo "<a href=\"$link\">$text</a><br>"; } $DB->Close(); } smenu(); ?> Menu.php is where the problem is. Now I've tried moving the required('mysq.php'); inside the function, but I think I read some where that that shouldnt have to be done. And according to code I've seen it shouldn't have to be done. But I've been trying to figure this out for a few days now and its driving me bonkers. Any help would be appreciated. Thanks, Zarithe Link to comment https://forums.phpfreaks.com/topic/44161-calling-a-mysql-class-from-within-a-function/ Share on other sites More sharing options...
genericnumber1 Posted March 25, 2007 Share Posted March 25, 2007 errrr your class is a bit off you never set $connection to anything, nor do you return it, also you later use it as $this->connection for mysql_close()... but this isn't your problem, your main problem is variable scope... try... <?php require('config.php'); class mysql { public $connection; function Connect($host, $mysql_user, $mysql_pass, $dbname) { $this->connection = mysql_connect("$host", "$mysql_user", "$mysql_pass") or die(mysql_error()); mysql_select_db("$dbname", $this->connection); } function Close() { mysql_close($this->connection); } function Query($sql) { $query = mysql_query($sql) or die(mysql_error()); return $query; } function FetchRow($query) { $rows = mysql_fetch_row($query); return $row; } function FetchArray($query) { $array = mysql_fetch_array($query); return $array; } function FetchNum($query) { $num = mysql_num_rows($query); return $num; } } ?> <? require('mysql.php'); function smenu() { globals $host, $mysql_user, $mysql_pass, $dbname; $DB = new mysql(); $DB->connect($host, $mysql_user, $mysql_pass, $dbname); $sql = "SELECT * FROM server_menu WHERE parent_id = 0 ORDER BY id"; $query = $DB->Query($sql); while($array = $DB->FetchArray($query)) { extract($array); echo "<a href=\"$link\">$text</a><br>"; } $DB->Close(); } smenu(); ?> I also fixed the problem I mentioned first your main problem was you forgot about variable scope Link to comment https://forums.phpfreaks.com/topic/44161-calling-a-mysql-class-from-within-a-function/#findComment-214666 Share on other sites More sharing options...
zarithe Posted March 25, 2007 Author Share Posted March 25, 2007 Thanks, genericnumber1. I didnt even think about the variable scope. Now I feel dumb lol. Question about setting $connection to a public... I admit that I'm just getting started with O.O.P. So I'm not quite sure why you said I never set $connection to anything. In my mysql class I do tell it that $connection = mysql_connect I know I'm a noob but gotta start some where heh? Link to comment https://forums.phpfreaks.com/topic/44161-calling-a-mysql-class-from-within-a-function/#findComment-214670 Share on other sites More sharing options...
genericnumber1 Posted March 26, 2007 Share Posted March 26, 2007 Well, yes, you set the connection to $connection, but you dont DO anything with it. To actually assign it to an instance variable you need to do $this->connection = foo(); or $this->connection = $connection; Link to comment https://forums.phpfreaks.com/topic/44161-calling-a-mysql-class-from-within-a-function/#findComment-215115 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.