How to access Wordpress' global $wpdb from PHP? - wordpress

I'm trying to execute a query in PHP against a MySQL database. My code is below:
<?php
require_once ("wp-includes/wp-db.php");
global $wpdb;
$myrows = $wpdb->get_results( "SELECT id, name FROM wp_db_posts" );
echo $myrows;
?>
After executing this code, I get a Fatal error: Call to a member function get_results() on null in C:\Apache24\htdocs\wordpress\me.php on line 4
I'm actually new to WordPress and want to know exactly how can I access the $wpdb global variable from PHP. My databses are pre-fixed with wp_db_.
What am I doing wrong? Can anyone help me here?

assuming you script placed in the root directory of WordPress you should load wordpress not access the wp-db.php directly so your code should be like this:
<?php
require_once "wp-load.php";
global $wpdb;
$myrows = $wpdb->get_results( "SELECT ID, post_name FROM {$wpdb->prefix}posts" );
and you can't echo myrows as you will get object not string you should use instead
var_dump() or print_r()

Related

how do you get current userid in wordpress

Hi have a simple php file in my wordpress site, where I need the current userid
I have tried the following, but keep getting
Call to undefined function get_current_user_id()
<?php
include '../../mydbfile.php';
global $wpdb;
// get current user ID, with default value, if empty
$current_user_id = get_current_user_id();
error_log('current_user_id '.$current_user_id);
You need to include wp-load.php to call the global $wpdb
<?php
require_once($_SERVER['DOCUMENT_ROOT'].'/wp-load.php');
global $wpdb;
// get current user ID, with default value, if empty
$current_user_id = get_current_user_id();
error_log('current_user_id '.$current_user_id);
This has been tested and works.

I cannot properly call $wpdb

I am working on a wordpress site with a custom theme, and I cannot figure out how to get $wpdb->prepare($query, $args) to work.
The Problem
I have a function in wp-content/themes/mytheme/php/functions.php which runs $wpdb->prepare($query, $args) to protect the query from SQL injection before executing the query and inserting some new data. However when I run this function, I get an error that reads "Call to a member function prepare() on null" on the line where $wpdb-prepare() is run. I did some googling and found that this means that $wpdb had not been defined.
What I tried
As per other threads I found online, I tried defining
global $wpdb
both inside my function and at the top of functions.php. When that didn't work I tried putting
include_one('/wp-includes/wp-db.php')
at the top of functions.php, but still nothing.
Does anyone have any other ideas about what I could try?
It's calling to the function just fine, but it's finding no valid input (null). What are you using to define your table name? I've had it before where a $wpdb won't run unless I've predefined it like this:
global $wpdb;
$table = $wpdb->prefix . "table_name";
$sql = $wpdb->prepare( "SELECT * FROM {$table} ORDER BY something DESC");
$result = $wpdb->get_results( $sql , ARRAY_A );

Get author's avatar inside a MySQL query in WordPress

I am fetching authors ID, points from wp_cp table using following code in my WordPress site and it works perfectly. Which I am trying now is to get users avatar beside users ID. How I can get users avatar inside the code?
<?php
global $wpdb;
/* wpdb class should not be called directly.global $wpdb variable is an instantiation of the class already set up to talk to the WordPress database */
$result = $wpdb->get_results( "SELECT uid,sum(points) as pt FROM wp_cp where timestamp between '2015-12-12' and '2015-12-31' group by uid "); /*mulitple row results can be pulled from the database with get_results function and outputs an object which is stored in $result */
//echo "<pre>"; print_r($result); echo "</pre>";
/* If you require you may print and view the contents of $result object */
echo "uid"." "."pt"."<br><br>";
foreach($result as $row)
{
echo $row->uid." ".$row->pt."<br>";
}
/* Print the contents of $result looping through each row returned in the result */
?>
If you use the uid field (which matches the user's ID in wp_users), pass it into the get_avatar function:
https://codex.wordpress.org/Function_Reference/get_avatar
Add the following code to get user avatar inside the foreach loop.
echo get_avatar( $row->uid );
Since you are asking for size, you can add a parameter to this function.
echo get_avatar( $row->uid, 64 ); // So the avatar size will be 64px X 64 px.
For further reference check this URL

Accessing plugin database table in functions.php

I'm trying to access a plugin's database from functions.php. With the code below, there are no errors, but there is no data echoing either. By looking around the web there are so many answers for so many different circumstances and none helped me apply any fixes to this particular code.
function show_review_count() {
global $wpdb;
$dbtable = 'wpcreviews';
$pageID = 4745;
$row = $wpdb->get_results("SELECT COUNT(*) AS `total` FROM `$dbtable` WHERE `page_id`=$pageID AND `status`=1");
echo $row[0]->total;
}
You're missing $wpdb's table prefix:
$dbtable = $wpdb->prefix . 'wpcreviews';

how to retrieve custom field at root directory for wordpress?

i have create a redir.php and placed in the home directory of wordpress to do the redirect. I want to pass in the post id and then retrieve a custom field value of the post and feed into header('location:'.$url);
www.mysite.com/redir.php?id=30
in redir.php will retrieve post id=30 custom field value and pass it into $url.
this is what I have, but it's not working. It gives me "Parse error: parse error in \redir.php on line 5".
it looks like wordpress environment is not being loaded.
<?php
require('./wp-blog-header.php');
$id = $_GET['id'];
$url= get_field('deal_http_link',post->$id);
header('Location:'.$url);
?>
thanks
Your script has multiple issues:
There is whitespace before the opening <?php tag, so the redirect wouldn't work because the headers will have already been sent. Instead, <?php should be the very first thing in the file.
post->$id is invalid syntax. You probably meant the $id variable which you defined in the preceding line.
To retrieve the value of a custom field, use get_post_meta(), not get_field().
Try something like this instead:
<?php
require('./wp-blog-header.php');
$id = $_GET['id'];
$url = get_post_meta($id, 'deal_http_link', true);
header('Location: ' . $url);
exit();

Resources