Well the most obvious thing is $database_vtconnection $vtconnection are defined nowhere in your getJobTypes function. If they're part of the class, you need $this (object) or self (static) references. More likely it looks like you're trying to use global variables, in which case you have to pull them into the scope of the function.
Up vote 0 down vote favorite share g+ share fb share tw.
The Situation I have a table in a DB that contains job types, basically defined by a label and a price. I'm trying to do a simple SELECT * FROM jobtype but I can't seem to get it to work, although I've use the same block over and over again in the rest of the code. The main difference here, is that it is a singleton trying to execute the function.
The problem is that as soon as I uncomment the line $rs_job_types = mysql_query($query_job_types, $vtconnection) or die(mysql_error()); the page will stop loading at this particular point in the code. The Code Following is the code of my function getJobTypes(): require_once('Connections/vtconnection. Php'); class JobTypes extends Singleton{ static private $job_types; public static function getJobTypes(){ if (self::$job_types == null){ echo 'DEBUG: For now, $job_types is NULL.
'. "\n"; mysql_select_db($database_vtconnection, $vtconnection); $query_job_types = 'SELECT * FROM jobtype'; $rs_job_types = mysql_query($query_job_types, $vtconnection) or die(mysql_error()); while ($rs_row = mysql_fetch_assoc($rs_job_types)){ // let the job type identifier in the db be its index in our array self::$job_types$rs_row'id''label' = $rs_row'label'; // job type label self::$job_types$rs_row'id''price' = $rs_row'price'; // job type price } if (self::$job_types! = null) echo 'DEBUG: $job_types has been populated.
'. "\n"; } return self::$job_types; } } Which I am calling like so: $jt = JobTypes::getJobTypes(); Here is my singleton pattern: class Singleton{ private static $instances = array(); final private function __construct(){ } final public function __clone(){ trigger_error('You don\'t clone a singleton! ', E_USER_ERROR); } final public static function getInstance(){ $c = get_called_class(); if(!isset(self::$instances$c)){ self::$instances$c = new $c; } return self::$instances$c; } } I have turned the problem in my head, commented everything inside the getJobtypes() function and uncommented step by step.
I found out the problem does happen with the mysql_query() line, just can't seem to work out why. Is something obviously wrong in my code? Solved As suggested here, I used global $vtconnection,$database_vtconnection; at the start of my static function and all went smooth.
It is not an optimal solution but it pointed out the scope issue which I will now try to resolve. I also got rid of the singleton pattern. Php class visibility global link|improve this question edited Jan 20 at 22:38 asked Jan 20 at 17:39Justin Huss206.
I would advise throwing away the Singleton base class and doing it in the JobTypes class. Consider whether you really should be using a singleton here, because most of the time you shouldn't. Singletons are bad for the same reasons that globals are.
A simple static function will usually suffice. – SystemParadox Jan 20 at 17:44 I'll try this in as soon as I get home. – Justin Huss Jan 20 at 17:53.
Well the most obvious thing is $database_vtconnection $vtconnection are defined nowhere in your getJobTypes function. If they're part of the class, you need $this (object) or self (static) references. More likely it looks like you're trying to use global variables, in which case you have to pull them into the scope of the function.
Mysql_select_db can auto-connect (if $vtconnection isn't supplied) but only if it knows how - there's a previous connection (or possible INI config with db/host/user/pass). To pull them into scope you need to add the line at the beginning of the function: global $vtconnection,$database_vtconnection;` ... Or use the $GLOBALS superglobal array: mysql_select_db($GLOBALS"database_vtconnection",$GLOBALS"vtconnection"); For the record using globals isn't a particularly good solution.
I only included snippets of the code in my example but I have a seperate vtconnection. Php file that I require_once() at the start of the page. I'll make sure it's not a scope issue by trying to echo all the variables I'm using.
– Justin Huss Jan 20 at 17:52 You say the opposite in your question - if that's not the total code of getJobTypes then don't say "Following is the code of my function getJobTypes()" – Rudu Jan 20 at 17:55 Sorry about the confusion. The getJobTypes() function is complete as you see it here, the vtconnection. Php is "required" in the global scope of the page.
– Justin Huss Jan 20 at 18:59 @Justin Huss That still doesn't bring the variables into the JobTypes::getJobTypes function – Rudu Jan 20 at 19:34.