I've seen other posts about this but I just can't understand them, what is the solution here, can you guys show me? Please. Static or not static? What does it mean? Is this the problem?
Db connection code:-
<?php
define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'root');
define('DB_PASSWORD', '');
define('DB_DATABASE', 'not telling');
class DB_con {
public $connection;
function __construct(){
$this->connection = new mysqli(DB_SERVER, DB_USERNAME, DB_PASSWORD,DB_DATABASE);
if ($this->connection->connect_error)
die('Database error -> ' . $this->connection->connect_error);
}
function ret_obj(){
return $this->connection;
}
}
Code for fetching records:-
<?php
$role_id = (isset($_SESSION['role_id']) ? $_SESSION['role_id'] : 4) ;
$query = "
SELECT rights_codename FROM permissions
INNER JOIN roles_and_permissions ON fk_permissions_id = permissions_id
WHERE fk_role_id = $role_id
";
$_SESSION['permissions'] = array();
$result = $this->db->query($query);
while($row = $result->fetch_assoc()){
array_push($_SESSION['permissions'], $row['permissions_cname']);
// $_SESSION['permissions'][] = $row['permissions_cname'];
}
if(in_array('admin_rediger_bruger',$_SESSION['permissions'])){
}
?>
Fatal error: Uncaught Error: Using $this when not in object context in C:\xampp\htdocs\R_L_E\login.php:30 Stack trace: #0 {main} thrown in C:\xampp\htdocs\R_L_E\login.php on line 30
In lay man terms, $this is when you are referencing a non static function in a class. What you can do is create a new instance of the database class and then use that variable created during instantiation to access the member functions of that class
Do this
$conn = new DB_con();//instantiate the class.add this at the very top of login.php
$conn->connection->query('your sql stuff');//replace the $this->db->query($query) with this line
You can access the connection property as it is declared as public in your class
Also do not forget to include the DB_con file
$this is a variable that refers to "the current object" when you're running code inside that object. That means, when you write this:
class AClass {
public function foo() {
var_dump($this);
}
}
$my_object = new AClass;
$my_object->foo();
Then $this is the same as $my_object during the execution of the foo() function. You can think of it like an extra parameter to the function, once you leave that function it won't exist any more.
So when you write this:
$result = $this->db->query($query);
You need to be inside some method which has been called on a particular object, so that there is an object for $this to refer to. If you're in global code, or a function that's not part of a class, or a static method, there is no "current object", so $this doesn't exist.
In your case, you're trying to get to some instance of a DB connection object. That means somewhere in your code you need to have created that object, and assigned it to a variable, then you can reference that variable. You can call that variable whatever you like, but you can't call it $this, because that's a reserved name.
Your DB_Con class doesn't have a query() method, so it looks like you want to get the MySQLi object out first, and then call the method on that.
So you would write something like:
$my_db_connection = new DB_Con;
$mysqli_connection = $my_db_connection->connection;
// or: $mysqli_connection = $my_db_connection->ret_obj();
$result = $mysqli_connection->query($query);
Or more concisely:
$my_db_connection = new DB_Con;
$result = $my_db_connection->connection->query($query);
// or: $result = $my_db_connection->ret_obj()->query($query);
Related
This question already has answers here:
Reference: What is variable scope, which variables are accessible from where and what are "undefined variable" errors?
(3 answers)
Closed 6 years ago.
I've been working on a databaseHandler php class, which should connect to the database and then be usable in other PHP files, atleast this was the plan. I've come across the problem that it cannot use any PDO related functions in my PHP class, i've tried checking if it was null, or not set at all (but it was) and i've also tried using a dummy function that just echos something which was to test if the class isn't undefined in others.
<?php
class databaseHandler {
private $db;
private static $instance;
function __construct() {
$this->buildDatabaseConnection();
}
public static function getInstance() {
if(!self::$instance) {
self::$instance = new databaseHandler();
}
return self::$instance;
}
private function buildDatabaseConnection() {
require 'dbconfig.php';
try {
$this->db = new PDO("mysql:host=" . HOST . ";dbname=" . DATABASE . ";charset=utf8", USER, PASSWORD);
if(isset($this->db)) {
if(!is_null($this->db)) {
echo "isn't null";
}
}
} catch(PDOException $e) {
$e->getMessage();
}
}
public function getConnection() {
return $this->$db;
}
public function getSomeShit() {
echo "some shit";
}
}
?>
The problem might be with the getConnection() php function, does it actually return the PDO object here? Because the main problem lays with the fact that i get this error when i use my getConnection function in other classes:
Notice: Undefined variable: database in F:\Websites\DevProject\php\functions.php on line 69
Fatal error: Uncaught Error: Call to a member function prepare() on null in F:\Websites\DevProject\php\functions.php:69 Stack trace: #0 F:\Websites\DevProject\php\functions.php(51): register('qwe', 'sdasd', 'asdasd', 'asdas', '01-01-1970') #1 F:\Websites\DevProject\register.php(123): datelessRegister('qwe', 'sdasd', 'asdasd', 'asdas') #2 {main} thrown in F:\Websites\DevProject\php\functions.php on line 69
and line 69 would be this:
$stmnt = $database->prepare("SELECT $username, $email FROM " . USERTABLE);
Where the $database variable is the class instance:
$database = databaseHandler::getInstance();
If i try to var_dump it in my registration php file, it'll tell me this:
object(databaseHandler)#1 (1) { ["db":"databaseHandler":private]=> object(PDO)#2 (0) { } }
However, when i use it inside of my function, it will say it is 'NULL', why is it null in a function but defined globally?
All the errors relate to the object, and in there it tells me the pdo is undefined (i've tried checking if it was null, but it wasn't!)
Any help would be great, thanks in advance.
In the return statement of getConnection method you should write the class variable without $:
return $this->db;
Also, you must do the queries on the PDO connection instead of your database handler class, so $database = databaseHandler::getInstance(); should be
$database = databaseHandler::getInstance()->getConnection();
or you can simply use the __call magic method to keep your code as it is right now:
public function __call($method, $args)
{
return call_user_func_array(array($this->db, $method), $args);
}
Also, to clarify how to use this class, you should call it inside every method you need the database to be used, not only once on the top of your file, because defining it there it will not be available inside your methods/functions unless you don't use the global statement to import the variable. However usage of the global variable is not encouraged and it is considered as a bad practice. An example on how to use the database would be:
public function doSomething()
{
$db = databaseHandler::getInstance()->getConnection();
$db->prepare(....);
}
public function doSomethingElse()
{
$db = databaseHandler::getInstance()->getConnection();
$db->prepare(....);
}
If you know there are many methods inside your class that uses the database then you can define it as a class variable and set the db instance in the constructor.
You seem to be having issues with scopes.
$database = databaseHandler::getInstance();
function doSomething()
{
// $database is not defined in this scope
$database->getConnection()->prepare(/* ... */);
}
You have to declare it as part of the global scope
function doSomething()
{
global $database;
$database->getConnection()->prepare(/* ... */);
}
The problem seems to be in the getConnection() function. Since $db is field you must do.
return $this->db;
However this is a very complex class structure for a simple problem.
You can directly extend your class from PDO
class DatabaseHandler extends PDO {
function __construct() {
parent::__construct('mysql:host=host_name;dbname=db_name', 'user_name', 'password');
parent::setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
}
And create an object of this whenever you want to do database transaction.
$db = new DatabaseHandler();
$stmt = $db->prepare(".....");
$stmt->execute();
I know this is an old question but I am not finding answer. Here is my code:
class SqlConnect implements connectionInterface {
//put your code here
private $datasource=null;
private $credentials = null;
public function __construct() {
$netsConn = new configInit(); // Here where the error is happing
$datasource = $this->$netsConn->getDatabase();
$credentials = $this->$netsConn->getBasicCredentials();
}
public function connect()
{
$conn = mysqli_connect($datasource['servername'], $credentials['username'], $credentials['password']);
if (!$conn)
{
die("Connection failed: " . $conn->connect_error);
} else {
return $conn;
}
}
}
I am then calling the class like
class applicationsController implements Controller {
private $conn = null;
public function __construct() {
$conn = new SqlConnect();
$this->$conn->connect();
}
...
}
However I am getting the following error:
Catchable fatal error: Object of class configInit could not be converted to string
This error happens in SqlConnect. I have looked through about 6 answers on stackoverflow and most all of them were about trying to echo some objects, which is not my case
Ahh, I think I see it.
You are assigning to global variable $netsConn, not object property $netsConn ...
Try this:
private $netsConn;
...
$this->netsConn = new configInit();
... that is, if you want to keep it around. Otherwise, refer directly to the variable not to a (never-declared ...) property.
The reason you get the error is because of the following 2 lines
netsConn = new configInit(); // Here where the error is happing
$datasource = $this->$netsConn->getDatabase();
First line is fine, but on the second line, you are using $netsConn as an attribute name for the SqlConnect class ($this). For this to work, php has to use the string representation of $netsConn by attempting to call it's implementation of the magic method __toString(). Since this probably does not exist in your configInit() class, you end up with that error.
So when I run this below it outputs an error Catchable fatal error: Object of class eg could not be converted to string
spl_autoload_register(function($class) {
require_once '../dbfolder/'.$class.'.php';
});
$mysql = dbWrapper::getDbInstance();
class eg
{
private $username = 'paul';
private $email = 'paul#yahoo.com';
public function eg_()
{
global $mysql;
$sql = "INSERT INTO users(username,email)VALUES(:username,:email)";
$prepared = $mysql->$this->handler->prepare($sql);
$prepared->bindParam(':username',$this->username);
$prepared->bindParam(':email',$this->email);
$prepared->execute();
}
}
$eg = new eg();
$eg->eg_();
Please can anyone just point out for me what am doing wrong?
This line is the problem:
$prepared = $mysql->$this->handler->prepare($sql);
PHP thinks you're trying to use it's variable variable feature. It sees $mysql->$this and tries to convert $this into a string, which the current class does not support (no __toString() magic method). I assume this is a typo/copy-paste error.
To fix it, all you need is to remove it (assuming your $mysql object has a property of handler that is):
$prepared = $mysql->handler->prepare($sql);
I'm trying to add a static member to each of my classes that contains the default database connection that they should use when instantiated. Here's how I'm trying to do it:
<?php //other classes extend Generic
class Generic {
public static $defaultDatabase;
public $db;
function __construct (&$pDatabase = null){
if ($pDatabase!==null)
$this->db = &$pDatabase;
else
$this->db = &$defaultDatabase;
}
}
?>
<?php
include_once("/classes/class.Database.php");
$db = new Database ("localhost", "username", "password", "TestDatabase");
$classes = array("Generic", "Member");
foreach ($classes as $class){
include_once("/classes/class.$class.php");
$class::defaultDatabase = &$db;//throws error here, unexpected "="
}
?>
What am I doing wrong? Is there a better way to do this, or do I have to set the defaultDatabase for each class individually? I'm using php 5.3, which I understand should support something like this.
In this code
$class::defaultDatabase = &$db
You should add $ before defaultDatabase, since static properties are accessed via
ClassName::$staticProperty
Unlike the others which are accessed via
$class->property;
Use self::$propertyName to access static properties:
function __construct (&$pDatabase = null){
if ($pDatabase!==null)
$this->db = &$pDatabase;
else
$this->db = self::$defaultDatabase;
}
Also note, that using the reference operator &$var is meaningless if $var is an object. This is because all objects in PHP are actually references.
I'm just changing my website's MySQL to PDO, and I've got a strange problem when I tried to use PDO in an other class.
class Database {
private $pdo;
public function __construct() {
$this->pdo = new PDO('mysql:host=localhost;dbname=appdora;charset=utf8', 'root', 'root');
}
}
class doClass {
//Variables
private $db;
//PDO
public function __construct(Database $db) {
$this->db = $db;
}
And the code is returns with: the following error:
Catchable fatal error: Argument 1 passed to doClass::__construct() must be an instance of Database, none given, called in .../index.php on line xx and defined in ../classes.php on line xx
The code:
$do = new doClass();
if ($do->loginCheck()) { echo 'loginOk'; } else { 'loginError'; }
loginCheck() is a simle function that works without classes!
Could you help me, what's the problem?
Thanks in advance!
$do = new doClass();
You defined your doClass class to expect a parameter in the constructor:
public function __construct(Database $db)
So you need to supply that parameter of type Database to successfully construct the object.
For example, if you have a database object stored before somewhere inside a variable $database, you can simply pass it to the constructor of doClass like this:
$do = new doClass($database);