PHP mysqli functions fatal ERROR when using the query function - php

I am programming a website and my current task is to save the text on a mysql server so I can login as admin and then edit the displayed text online.
I already got so far that everything works but I have done it using mysql functions. Now changed all those functions to mysqli ones, since I heard those are way better.
Here is my mysql_connect.php which I created so I could include this in the other scripts wherever I need to connect to the server:
mysql_connect.php
This is my block1_aboutme.php file, where I connect to the server and request all the text from it so I can display it on my "about me" page. This also works as intended and it properly displays the text on my page.
block1_aboutme.php
The problem is when I login as an admin a part of the login verification is this code below in my class.login.php script. As soon as I am logged in and go to my aboutme page an error occurs:
Fatal error: Uncaught Error: Call to a member function query() on unknown in C:\xampp\htdocs\mywebsites\LuPaw\admin\class.login.php:84 Stack trace: #0 C:\xampp\htdocs\mywebsites\LuPaw\admin\class.login.php(73): Login->verifyDatabase() #1 C:\xampp\htdocs\mywebsites\LuPaw\admin\class.login.php(36): Login->verifySession() #2 C:\xampp\htdocs\mywebsites\LuPaw\aboutme.php(42): Login->isLoggedIn() #3 {main} thrown inC:\xampp\htdocs\mywebsites\LuPaw\admin\class.login.php on line 84
This error refers to line 84 which is the $data = $db-> query("select stuff") function.
Somehow this does not work here, while the nearly exact same code line does work in the block1_aboutme script.
I would place the link of my 3rd script here but I can't since I don't have reputation pints yet to place 3 links in one post. Will post 3rd picture in the comments.
I already tried out a few things. If I for example echo out the "$path" it shows the correct path which means $db should be a viable variavble.
Does anyone see the problem here?

Try not to use include_once in your class method as the file will not be included if anywhere during the script execution has been already included: use include instead.
Anyway this is not the best solution you can achieve, I can suggest you to implement a DB class as a singleton and then retrieve the database instance in order to execute your queries.

Related

Call to a member function get_results() on null

I'm building a Wordpress site and for some application, an external website needs to access one of my page destination.php by the URL adress. So, I place this page at the root of my website. This page destination.php needs to access a table from my database. I thought using $wpdb would be enough to access any table but this variable does not seem to be initialized as a get the following error
Uncaught Error: Call to a member function get_results() on null
Following various advice from this forum, I tried :
require_once('../wp-config.php');
Then I get this error
Warning : require_once([...]/wp-settings.php): failed to open stream: No such file or directory in [...]
As advised for security reasons, wp-config is not in the root folder and so wp-config and wp-settings are not at the same level. Thus wp-config does not seem to be able to find wp-settings. I don't know if (and how) I should modify wp-config to provide it with the right path
require_once('wp-load.php');
Then I have no error and my script stops running. Thus I don't know what it's going wrong with wp-load. It is in the same folder as my file destination.php.
A minimal example is as follows
<?php
//require_once('../wp-config.php');
require_once('wp-load.php');
echo 'loading complete';
global $wpdb;
$myrows = $wpdb->get_results( "SELECT * FROM my_table" );
?>
Any help would be much appreciated. I've been stuck there for many days...

Fatal error caused by get_theme_mod()

I've been using the WordPress Customize.php functionality to allows users to customize my theme via the admin Customizer. It's been working fine offline in WAMP, but when I uploaded the theme to the testing server I received this message:
Fatal error: Can't use function return value in write context in [my url]/functions.php on line 181
The line it links to is a reference to get_theme_mod(), and in particular:
$page_data = get_page(get_theme_mod('page'));
Do I have to register the 'theme mods' in functions or something?
Right, I've found my issue - and another one has arose that I'm looking into...
For those that read this after searching the web having suffered the same problem, the reason I was receiving this error was because above this I was testing the get_theme_mod with an empty() function. Which, does not work as get_theme_mod is not a variable.
if(!empty(get_theme_mod('page'))){
$page_data = get_page(get_theme_mod('page'));
}

Requesting assistance with 1 line of code regarding xajax

I am attempting to build a PHPMailer based email system for a basic website.
This is the line of code that is giving me trouble:
$xajax->printJavascript('xajax/');
Now, this is the tutorial I am using.
Regarding the above line of code, the tutorial says this:
How to use the code inside a webpage?
Place the form (variable), the function (and the includes) before of all html code. Next we need to include some JavaScript file in the documents HTML header (place also php tags):
$xajax->printJavascript('xajax/');
When I run all of the code (including: PHPMailer script; Ajax script), I get this error, on the aforementioned line of code.
Fatal error: Call to a member function on a non-object
So, my question is, do I need to in someway customize this code or make it run to a filepath of some ajax core file or something?
I would be willing to be $xajax is not defined. Try adding this after including the libraries but before including the call to that method:
$xajax = new xajax();
There may be some additional setup required by xajax. A more complete code example could help troubleshoot.

PHP - Error 500 after page refresh

I've got a PHP application and I'm having trouble getting a User object from the SESSION.
When saving the object I have the following code (some other file).
$_SESSION['user'] = serialize($user);
The problem begins when I try and load a file called timeline.php. The first time I load the page (after I've logged in and the SESSION['user] is set) the page works fine. When I refresh the page I get an error 500.
The error log shows this:
Call to a member function get() on a non-object in timeline.php on line 10.
The code from line 2-10 is this:
include_once('isUserLoggedIn.php');
require_once('classes/User.php');
session_start();
$user = unserialize($_SESSION['user']);
require_once('classes/Database.php');
$tweets = Database::getTimeline($user->get('user_id'));
Can anyone offer any reason for this?
UPDATE (1)
In timeline.php I added var_dump($user) to line 8. The first time I load the page it gives me the correct value.
The second time I load the page it gives me bool(false).
UPDATE (2)
The problem seems to be with the host (perhaps a setting in the php.ini file there). The company I was using is Fatcow. When I used someone elses host (1and1) the issue didn't happen.
When unserializing objects lots of strange things can occur, see the PHP manual page for more information.
http://php.net/manual/en/function.unserialize.php
This comment seems pertinent:
http://www.php.net/manual/en/function.unserialize.php#77826

Which file path and line number to show in log?

I have written a logging library that will add entries to a file or output stream.
When the entry is added to the log, it also includes context information such as the file and line number of the call that was made to add the entry.
My question is: Should I use the file path and line number of the call that added the entry, or the caller that triggered the event to log the message?
trigger.php:
<?php
$do->something(); #line 2
do.php
<?php
class Do
{
public function something ()
{
$this->log->add('Did something.', E_USER_NOTICE); # line 6
}
}
Right now, the message "Did something." is logged as having come from do.php line 6. Is it better to change the logging class to say that the message came from trigger.php line 2?
EDIT
Clarifying a few things.
The library includes the whole call stack in the log entry. So in more feature rich logging classes, you can see a lot more than what the example above entails.
The library does make use of logging levels and will filter out levels that aren't needed. The example above does not show that, but you can look at the library itself to know for sure.
Unfortunately I wasn't clear on how the message is being represented in this one scenario. This very minor scenario will only show one file path and one line. Assuming this could not be changed, which file path and line number would be logged?
Thank you for your help!
Also, what if you have three level instead of two ? What if you have four ? I recommend you to go with the conventional practice of keeping trace of the line where the actual log() call appears.
class client
{
public function get_some_data()
{
$data = $this->get_my_data();
if (empty($data)) {
$this->log->add('no data in sight');
}
// lots of processing
if ($something) {
$this->log->add('data was corrupted');
}
}
}
//...
$client->get_some_data();
If you only log the upper level call, you would only know that it happened in get_some_data() but not where exactly. You might think "Yes but I can find it using the exact error that was logged", but when you are digging logs to pinpoint a problem that is really not what you want to be doing.
If you have several call to this function and you want more informations about which one logged the line, what you can do is store a minimal stack trace alongside your log, see debug_backtrace(). It can be as minimal as
2011-12-25 17:02:37 ERROR "data was corrupted" at /path/client.php:6 (called from /path/somefile.php:57)
I would also strongly recommend that you add different logging level, because the logging you want in dev/debug situation is not the one you want in production.

Categories