Fatal error: Call to a member function get_row() - php

I'm working for admin login please anybody fix that..
Email: <?php echo $admin->get_email(); ?>
Fatal error: Call to a member function get_row() on a non-object in D:\MyWebSite\business_design\admin\admin-class.php on line 82
The code:
public function get_email() {
$username = $_SESSION['admin_login'];
global $db;
$info = $db->get_row("SELECT `email` FROM `user` WHERE `username` = '" . $db->escape($username) . "'");
if(is_object($info))
return $info->email;
else
return '';
}

Okay, are you using a framework?
Your $db variable is not instantiated, so when you call $db->get_row:
PHP can't find the $db object; and so,
get_row() can't exist.

To start with, make sure you know which class $db should be referring to. The same class will have the function "get_row"
First you have to include that class file in your php scrip file. That can be done easily by following script -
function __autoload($class_name){
require_once("RELATIVE_ADDRESS_OF_THE_CLASS".$class_name.".php");
}
Say for example you have your class is Database, so you should first instantiate $db as below -
$db = new Database();
Later if all the scripts are working properly in "Database" class, it should work straightaway...
Hope it helps..

Related

Uncaught Error: Using $this

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);

PHP OOP, undefined property. How can I get this to work?

Okay so I'm basically trying to improve with PHP OOP, however I'm not too sure how I should do this. Could someone please point out the issue?
The code returns the following errors:
Notice: Undefined property: registration::$userExist
Fatal error: Call to a member function fetch() on a non-object
The class:
class registration extends connect{
public function userExist(){
global $dsn;
$userExist = $this->dsn->prepare("
SELECT * FROM accounts
WHERE username= :username
");
$userExist->bindParam(':username', $username);
$userExist->execute();
$rows = $this->userExist->fetch(PDO::FETCH_NUM);
return $rows;
}
How I'm trying to use the class on a page:
$conn = new connect();
$registration = new registration();
$rows = $registration->userExist();
if($rows < 1){
// do something
The error says that it cannit find variable $userExist, this is because on the last line you are looking for variable "$userExist" inside "$this" while you defined it as a normal variables a few lines above it.
To fix the problem, drop the this-> part of the line that gives the error so it pick up the variable from the local scope
$rows = $userExist->fetch(PDO::FETCH_NUM);
EDIT
You said in comments that variable $username was properly defined before you called the method userExists(). However, in php variables won't be copied over to inside of the function code block when they are called. To pass variables you need to add an argument to the function.
public function userExist($username){
...
$rows = $userExist->fetch(PDO::FETCH_NUM);
....
}
And you need to use it like:
....
$rows = $registration->userExist($username);
....
You don't need $this->userExist
class registration extends connect{
public function userExist(){
global $dsn;
$this->dsn = $dsn; // assign global $dsn to $this->dsn
$userExist = $this->dsn->prepare("SELECT * FROM `accounts` WHERE `username`= :username");
$userExist->bindParam(':username', $username);
$userExist->execute();
$rows = $userExist->fetch(PDO::FETCH_NUM);
return $rows;
}
This happens because you don't have a property userExist in the class registration.
Just change
$rows = $this->userExist->fetch(PDO::FETCH_NUM);
for
$rows = $userExist->fetch(PDO::FETCH_NUM);

accessing a global php object in a function is resulting in error

$db=new DataBase();
It is a object I created globaly. For accessing this in a function , I used global keyword.
Here is the code:
include "Database.php";
$db=new DataBase();
function getUser()
{
$uname=$_SESSION['UNAME'];
global $db,$uid;
$result2=$db->selectUserDetails();
$result3=$db->selectUserPermission($uid);
$table = constructTable($result2, $result3);
echo $table;
}
and when i am using that, it is showing an error
"Fatal error: Call to a member function selectUserDetails() on a non-object
in C:\\wamp\\www\\listdetails.php on line 27"
Anyone can tell me a solution.
This sounds to me like you are calling a static function in a non static context..
I think you need to nix the new DataBase() and just call the function statically;
$result2=$db::selectUserDetails();

CodeIgniter: get_instance inside My_Lang

I found this useful Internationalization code:
http://pastebin.com/SyKmPYTX
everything works well except I am unable to use CI functions inside this class .
I want to set $languages and $special variable from DB .
but when I am using $CI =& get_instance(); in instance function its showing following error :
Fatal error: Class 'CI_Controller' not found in /system/core/CodeIgniter.php on line 231
The language class is loaded before the CodeIgniter instance exists, which is why you get the error.
You can use a post_controller_constructor hook to set your variables.
Here is a thread from the CodeIgniter forums where someone is tried to do something similar: http://codeigniter.com/forums/viewthread/108639/
The easiest way
in My_Lang.php
var $languages = array();
function __construct()
{
parent::__construct();
require_once( BASEPATH .'database/DB'. EXT );
$db =& DB();
$query = $db->query( 'SELECT * FROM languages');
$result = $query->result();
foreach( $result as $row )
{
$this->languages[$row->short_name] = $row->full_name;
}
}
i did this and is working fine :)) i also added default_uri in foreach.

Trying to get property of non-object php error [duplicate]

This question already has an answer here:
Closed 11 years ago.
Possible Duplicate:
Call to a member function on a non-object - works localhost but not online
don't know how to solve this problem, it's basic but can't find a solution.
I use two files: connect.php and user.php.
in connect.php is a Connect class:
class Connect{
var $logged;
function login($username, $password){
..more code..
if($pass==$password){
$this->logged=true; // db connection is fine, this works
// checked it with echo $this->logged;
}
}
}
and when i call it from another file, user.php like this:
$user=new Connect;
$user->login();
echo $user->logged; // ERROR Trying to get property of non-object
Why is this code not working, but it works offline (locally)???
You've got a couple of problems:
When you call $user->login, PHP is assuming you're accessing an object property, not the function; you need $user->login() (note the ()), which will call the method.
Your example is missing a }.
Demo:
<?php
class Connect{
var $logged;
function login($username, $password){
$pass = 'test';
if($pass == $password){
$this->logged = true;
}
}
}
$user = new Connect;
$user->login('test','test');
print_r($user);
?>
http://codepad.org/AVw0k9sY
Outputs:
Connect Object
(
[logged] => 1
)
1 is what true prints.
To access a class property from outside the class, you need to declare it as 'public':
class Connect{
public $logged;

Categories