We have one custom plugin page. That page file path is – wp-content\mu-plugins\custom_api_logs_list.php
We wrote the coding to list all our API logs from database logs table and list in the table format – look here – https://prnt.sc/Swje1VdgkVDh
This page file having code to get data from logs table with offset & limit. But facing the slow php error in php slow log – https://prnt.sc/91W0Y-nAtv8r
and please see other errors also in the screenshot.
Due to the error, the page is not loading, its taking long time and display timeout error ‘The application did not respond in time’.
Here is some screenshot from coding –
https://prnt.sc/WZhGktRewP3X
https://prnt.sc/rtcaMBwU2ME4
https://prnt.sc/OlaBSDQ6d7cX
https://prnt.sc/LkGFGEf1ayvO
1. If I try print_r or echo before get_var() query, everything is working, no slow in the page. But get_var only not working. All other remaining is working.
2. I tried get_results() instead of get_var() but thats also triggered error & not loading
Its happened in WP 6.0.2 only i think.. Bcz before in old version, we didn’t face this issue.
Can anyone please help.
]]>See the code below:
$query = $wpdb->get_results(
"SELECT ID, post_title, post_content, post_excerpt, guid
FROM $wpdb->posts
WHERE post_status = 'publish' AND post_type = 'page' AND
post_title LIKE '%$get_search_query%' OR post_content LIKE '%$get_search_query%'
ORDER BY ID ASC"
);
I can search the page, but wanted using the same code to identify the language and reduce the search in the database. That is, where is the language of the statement in the post?
Again, thank you!
$minAge = xprofile_get_field_data(3471, bp_loggedin_user_id());
$maxAge = xprofile_get_field_data(7475, bp_loggedin_user_id());
$age_q = "SELECT GROUP_CONCAT(distinct ".$wpdb->base_prefix."bp_xprofile_data.user_id) as user_ids FROM ".$wpdb->base_prefix."bp_xprofile_data JOIN ".$wpdb->base_prefix."bp_xprofile_fields ON ".$wpdb->base_prefix."bp_xprofile_data.field_id = ".$wpdb->base_prefix."bp_xprofile_fields.id WHERE field_id =2 AND ".$wpdb->base_prefix."bp_xprofile_data.user_id !=".bp_loggedin_user_id();
$agewhere = "";
if($minAge){
$agewhere .= " AND TIMESTAMPDIFF( YEAR, STR_TO_DATE( ".$wpdb->base_prefix."bp_xprofile_data.value, '%Y-%m-%d' ) , CURDATE( ) ) >= ".$minAge;
}
if($maxAge){
$agewhere .= " AND TIMESTAMPDIFF( YEAR, STR_TO_DATE( ".$wpdb->base_prefix."bp_xprofile_data.value, '%Y-%m-%d' ) , CURDATE( ) ) <= ".$maxAge;
}
var_dump($age_q.$agewhere);
if(!empty($agewhere)){
$ageResults = $wpdb->get_var($age_q.$agewhere);
var_dump($ageResults);
}
Query Generated by var_dump function like below \
"SELECT GROUP_CONCAT(distinct wp_bp_xprofile_data.user_id) as user_ids FROM wp_bp_xprofile_data JOIN wp_bp_xprofile_fields ON wp_bp_xprofile_data.field_id = wp_bp_xprofile_fields.id WHERE field_id =2 AND wp_bp_xprofile_data.user_id !=5 AND TIMESTAMPDIFF( YEAR, STR_TO_DATE( wp_bp_xprofile_data.value, '%Y-%m-%d' ) , CURDATE( ) ) >= 18 AND TIMESTAMPDIFF( YEAR, STR_TO_DATE( wp_bp_xprofile_data.value, '%Y-%m-%d' ) , CURDATE( ) ) <= 31"
Have a $wpdb->get_results call like this:
$currentProductsArray = array();
$currentProducts = $wpdb->get_results(“SELECT id, feedid, size, price FROM products WHERE shopid = $shopid”);
Everything is fine if a shop just have a few thousand products, but some shops have 100,000+ products => the array gets huge and causes the script to crash.
Anyone got an idea for saving the results of $wpdb->get_results in a smaller data structure than the standard PHP array?
Thanks,
Mads
I am trying to retrieve all user meta (in columns) for each users (in rows).
I build (not without pain) this query that is working in phpmyadmin :
SET group_concat_max_len=16384; SET @sql = NULL; SELECT GROUP_CONCAT( DISTINCT CONCAT( 'MAX( IF ( M.meta_key = ''', meta_key, ''', M.meta_value, NULL)) AS `', meta_key, '`' ) ) INTO @sql FROM wp_usermeta; SET @sql = CONCAT( ' SELECT *, ', @sql, ' FROM wp_users as U, wp_usermeta as M WHERE U.id = M.user_id GROUP BY U.id ' ); PREPARE stmt FROM @sql; EXECUTE stmt;
But when I execute it with $wpdb->get_results, it returns an empty array.
Anyone can help ?
]]>$episode = $_POST['episode'];
$is_in_database = $wpdb->get_results(
$wpdb->prepare("SELECT object_id FROM wp_term_relationships WHERE term_taxonomy_id = %d AND object_id IN (SELECT ID FROM wp_posts WHERE post_title = '1x04')", $get_term_taxonomy_id, $episode)) or die('Error');
Is working fine but whenever i am trying to use variable $episode it does not.
$episode = $_POST['episode'];
$is_in_database = $wpdb->get_results(
$wpdb->prepare("SELECT object_id FROM wp_term_relationships WHERE term_taxonomy_id = %d AND object_id IN (SELECT ID FROM wp_posts WHERE post_title = %s)", $get_term_taxonomy_id, $episode)) or die('Error');
]]>I’m running into a wall because both latitude and longitude are meta keys and the code below is only outputting one meta value per entry. So it’s creating two entries per post, one with lat and one with long.
Any advice would be greatly appreciated
<?php
$lat = 'latitude';
$long = 'longitude';
$results = $wpdb->get_results( "
SELECT
post_title,
meta_value
FROM {$wpdb->posts}
INNER JOIN {$wpdb->postmeta}
ON ( $wpdb->posts.ID = {$wpdb->postmeta}.post_id )
WHERE meta_key = '$lat'
OR meta_key = '$long'
" );
foreach( $results as $result )
{
printf( '<pre>%s</pre>', htmlspecialchars( var_export( $result, true ) ) );
}
?>
<style type="text/css">
html, body { height: 100%; margin: 0; padding: 0; }
#map { height: 350px; width:100%; }
</style>
<script type="text/javascript">
// MAP
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 11,
center: {lat: 53.343156, lng: -6.258545},
scrollwheel: false
});
setMarkers(map);
}
// LOCATIONS
var locations = [
<?php
foreach( $results as $result ) {
echo "\t\t\t['"
. str_replace("'", "\'", $result[0])
. "', " . $result[1]
. ", '" . $result[3] . "'"
. "],\n";
} ?> ];
function setMarkers(map) {
// Adds markers to the map.
for (var i = 0; i < results.length; i++) {
var result = results[i];
var marker = new google.maps.Marker({
position: {lat: result[1], lng: result[2]},
map: map,
title: result[0]
});
}
}
</script>
<script async defer src="https://maps.googleapis.com/maps/api/js?key=MyKey&callback=initMap">
</script>
]]>I’m developing a custom plugin and I’ve got to retrieve some numbers from a table where the value in the expiry_date
column is greater than value that I’ve stored in a variable called $scheduleddate (That is, I need to retrieve numbers that haven’t expired as at the $scheduleddate). Here is what I’ve tried:
$scheduled_date = strtotime($year."-".$month."-".$day);
$scheduleddate = date('Y-m-d',$scheduled_date);
global $wpdb;
$table_name = $wpdb->prefix . "subscribers";
$result = $wpdb->get_results("SELECT number FROM " . $table_name .
" WHERE list_id=" . $list . " AND expiry_date > " . $scheduleddate);
When I tested it, I still ended up with the numbers that had already expired (I echoed the numbers so that I could see whether they were the ones I needed). I need to retrieve the numbers that are NOT expired as at the time in the $scheduleddate. What’s the correct way to write the query? The expiry_date
column is in the date format in the database table. (And if it’s important, the $year
, $month
, and $day
variables are gotten from $_POST
and I need them that way.)
Am I meant to use $wpdb->prepare
as well?
Thanks in advance!
]]>The query is a preset CSV download of 2 tables, (it’s actually a list of artists and their exhibition venues, which may or may not be known) :
$SQL = "SELECT a.forename, a.surname, a.address_1,
a.address_2, a.address_3,
a.postcode, a.phone, a.phone2, a.email, a.website_url,
v.address_1, v.address_2, v.address_3, v.postcode,
a.wbat_artist_id, a.reg_date
FROM wbat_artist a
LEFT JOIN wbat_venue v ON ( a.venue_id = v.venue_id )
WHERE a.is_current_exhibitor = 1
ORDER BY reg_date, wbat_artist_id";
$results = $wpdb->get_results( $SQL , ARRAY_N);
$i = 16;
foreach ($results as $row)
{
for ($j=0;$j<$i;$j++)
{
$csv_output .= '"'.str_replace('"','',stripslashes($row[$j])).'",';
}
}
The $wpdb->get_results method doesn’t include columns from the second table (aliased ‘v’) even where they do exist, but issues notices of undefined indexes 12,13,14,15.
Is there something wrong with the equality operator in the JOIN part? It’s as though it never comes up true, though both columns are int(11)s and the condition is met for most of the rows. I have read in other posts of ‘limitations’ with $wpdb on custom queries but no clarification of what these limitations are.
Thanks for any help.
]]>$totalsdata = $wpdb->get_results( $wpdb->prepare( "call dl_db_totals('%s', '%s', %d);", $startdate, $enddate, $current_user->ID ) );
print_r($totalsdata);
$totalsdata = $wpdb->get_results( $wpdb->prepare( "call dl_db_totals('%s', '%s', %d);", $startdate, $enddate, $current_user->ID ) );
print_r($totalsdata);
The first result prints a full array of values, the second prints an empty array (i.e. Array ()).
The context for this when a you need to call $wpdb->get_results() in a shortcode and the author includes more than one shortcode in a post.
]]>