Hi, I got this class: <?php class db{ public $conn; public $dbs; function connect( $host, $user, $pass, $name ){ $this->conn= mysql_connect($host, $user, $pass) or die(mysql_error()); $this->dbs= @mysql_select_db($name) or die(mysql_error()); } function exec($query){ if($this->conn){ $q= mysql_query($query) or die(mysql_error()); return $q; } } }//class end ?> Code (markup): Then I do the following: $db= new db; $db->connect($host, $user, $pass, $database_name); $s= $db->exec(" some query"); Code (markup): and I get this error: Fatal error: Using $this when not in object context in /path/db.php on line 21 Line 21 is: if($this->conn){ What am I doing wrong here? I want to check if a connection is made to the database or not before sending the query. If I cannot do it like this, how should I do it? Thanks
I don't see any problem in your code, the only thing I can see, you using the reserve function name exec in your function decrelation. try rename that function to other name and try again. Change the following function name to other name function exec(){ }
function connect( $host, $user, $pass, $name ){ $this->conn= mysql_connect($host, $user, $pass) or die(mysql_error()); $this->dbs= @mysql_select_db($name) or die(mysql_error()); } Double check your connect function to make sure it's connect, check your database name as well.
ini_set('display_errors', 1); class db{ public $conn; public $dbs; function connect( $host, $user, $pass, $name ){ $this->conn= mysql_connect($host, $user, $pass) or die(mysql_error()); $this->dbs= mysql_select_db($name) or die(mysql_error()); } function exec($query){ if($this->conn){ $q= mysql_query($query) or die(mysql_error()); return $q; } } }//class end PHP: Remove the @ from mysql_select_db and test your code. The main reason I think the error occur because there's an error while building the object db in your connect function, and your code hang with incomplete object. The error might cause by invalid login or database name.
Hi, Removed the @ sign, but still no use. If I remove this line: if($this->conn){ then the entire script works properly, executing the mysql query and getting output from database. But as soon as I put that line back, the same script gives a fatal error. The database connection is made otherwise the script won't work without that line as well. Thanks
Hi, Found the error. In case someone else makes this mistake, here's what happenned-- There's a seperate function in another class which is making a call to this function, and it's doing it like this: $s= db::run_this(" some query "); and not like: $s= $db->run_this(" some query "); This is why I'm not able to use $this->conn when calling the function like: db::run_this() Changed it to: if($conn){ and it works now. Is there some other way to access $conn from another class? $conn is a variable in "class db", but if I want to access it in "class something_else", how do I do it? Thanks
$db = new db; $page = new Page; class Page{ public function __construct(){ global $db; $query = 'SELECT * FROM JEET'; $result = $db->exec($query); while($j = mysql_fetch_array($result)){ echo $j['bike']; echo $j['car']; echo $j['tshirt']; echo $j['cake']; } } } PHP: