Call to a member function get_results() on null - php

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...

Related

Use of undefined constant ABSPATH

I am using WP for the first time. I'm just trying to create a very basic script to echo the user's id and am having all sorts of issues.
The code is this and is currently located in wp-content/plugins (i'm not really sure where these things should be):
<?php
require_once ABSPATH . '/wp-includes/pluggable.php';
$user = wp_get_current_user();
echo $user->get_site_id();
I'd had it without the require initially but I was getting a function not defined error for wp_get_current_user. Now I'm getting Warning: Use of undefined constant ABSPATH - assumed 'ABSPATH'...
Is there some sort of predefined set of files that I need to include or some specific directory I need to be putting my scripts so that these variables and functions are in scope? My understanding was that these things are supposed to be global.
Did you try code like that:
add_action('init', 'some_function_name');
function some_function_name() {
$user = wp_get_current_user();
echo $user->get_site_id();
}
The WordPress comes with hooks (actions and filters) to let other developers modify either core parts of the WordPress or code from other plugins / themes.
The code I describe in my answer, it is running your code when the whole WordPress , all the Plugins and the theme are loaded, thus you should have by default the wp_get_current_user() function and you should not need to include manually the pluggable.php.
This seems like is going to solve your problem.
Read more on hooks here: https://developer.wordpress.org/plugins/hooks/.
Side note. Keep in mind that in order to run your custom code you should register a proper WordPress plugin and activate it. If you have made a php file in the plugins folder, and you loaded using PHP functions like require/include the plugin probably will not operate as you expect although the source code it could be perfect. To get more details on how to write your own plugin, you could read here: https://developer.wordpress.org/plugins/

PHP mysqli functions fatal ERROR when using the query function

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.

Including plugin files into main plugin file properly

I've been having this issue for a while but keep just working around it an thought I'd finally get it solved.
I'm trying to include files into my main plugin document (the one that has the plugin title and version in it) like this:
define('SBT_PLUGIN_URL', plugin_dir_url(__FILE__));
include(SBT_PLUGIN_URL . 'competition_table.php');
inside the competition_table.php is an add_shortcode(); function that needs to run, in order for the shortcode to be registered with wordpress:
function add_table() {
//Run code here
}
add_shortcode('competition_table', 'add_table');
When I run the code on the site the link resolves properly, including the correct file, however I get this Fatal Error:
Call to undefined function add_shortcode()
However if I add exactly the same code that is in the competition_table.php into my main plugin document then the code runs perfectly.
So basically, my question is, why is Wordpress not recognizing it's own function and how can I include the file to make the code run properly?
Thanks in advance
You have to develop with WP_DEBUG enabled. It dumps an error: wrapper is disabled in the server configuration. That lead me to this: "Trust me, you do not want to include from URLs.".
Then I realized you're defining that constant with plugin_dir_url(), when what you need is a path. The following magic constant does the job:
include_once __DIR__ . '/competition_table.php';
Thanks to the feedback from #b__ I have managed to solve this issue.
For some reason, Magic Constants don't always work with wordpress, however, you can use it's equivalent to get the same effect:
include_once dirname(__FILE__) . '/competition_table.php';
When including files for use in a wordpress plugin you should always include via a PATH, not by a URL.

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

Wordpress loop inside of function = fatal error

I have my Wordpress blog on blog.mysite.com, and a totally different site (built in my framework) at www.mysite.com.
I know if the blog and another site are on the same server, and have correct permissions, I can use the following to "syndicate" my blogs to the non-blog site with:
define('WP_USE_THEMES', false);
require('/var/www/vhosts/mysite/subdomains/blog/httpdocs/wp-config.php');
query_posts('showposts=5');
...and then run a loop on the page.
The problem is that since my non-blog site is a framework, everything except for my front controller lives in a function - and this is creating the following error for me:
Fatal error: Call to undefined method
stdClass::set_prefix()
(btw, I'm fairly certain the fact that this code is called within a function that is causing the error...when I put the code on the front controller (not in function), the error disappears)
I would REALLY like to have recent blog listings on my non-blog site to spruce it up. Any ideas on how to do this?
Well I guess you forgot to write
global $wpdb
inside the function

Categories