PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, December 29, 2021

[FIXED] Wordpress: 500 Internal Server Error, probable issue using $wpdb

 December 29, 2021     mamp, mysql, php, wordpress     No comments   

Issue

I'm using Wordpress and I'm performing a query which gives me back this error:

Failed to load resource: the server responded with a status of 500 (Internal Server Error)

My query looks like:

global $wpdb;
        $session_uid = isset($_POST["session_uid"]) ? trim(strip_tags($_POST["session_uid"])) : "";
        $the_data = isset($_POST["the_data"]) ? trim(strip_tags($_POST["the_data"])) : "";
        $ss = "select * from ".$wpdb->prefix."vp_pms_messages inner join ".$wpdb->prefix."vp_pms_group_users on ".$wpdb->prefix."vp_pms_messages.id = ".$wpdb->prefix."vp_pms_group_users.message_id and ".$wpdb->prefix."vp_pms_messages.group_id = ".$wpdb->prefix."vp_pms_group_users.group_id where ".$wpdb->prefix."vp_pms_group_users.from_username = '$session_uid' and ".$wpdb->prefix."vp_pms_group_users.from_del = '0' or ".$wpdb->prefix."vp_pms_group_users.to_username = '$session_uid' and ".$wpdb->prefix."vp_pms_group_users.to_del = '0' group by ".$wpdb->prefix."vp_pms_messages.group_id";
        $check_last_conversation = $wpdb->get_results($ss);
        $response = print $check_last_conversation;

I'm probably missing or misunderstanding something but if I comment out $check_last_conversation and I print something like "Hello", the error goes away.

This is ok:

global $wpdb;
    $session_uid = isset($_POST["session_uid"]) ? trim(strip_tags($_POST["session_uid"])) : "";
    $the_data = isset($_POST["the_data"]) ? trim(strip_tags($_POST["the_data"])) : "";
    //$ss = "select * from ".$wpdb->prefix."vp_pms_messages inner join ".$wpdb->prefix."vp_pms_group_users on ".$wpdb->prefix."vp_pms_messages.id = ".$wpdb->prefix."vp_pms_group_users.message_id and ".$wpdb->prefix."vp_pms_messages.group_id = ".$wpdb->prefix."vp_pms_group_users.group_id where ".$wpdb->prefix."vp_pms_group_users.from_username = '$session_uid' and ".$wpdb->prefix."vp_pms_group_users.from_del = '0' or ".$wpdb->prefix."vp_pms_group_users.to_username = '$session_uid' and ".$wpdb->prefix."vp_pms_group_users.to_del = '0' group by ".$wpdb->prefix."vp_pms_messages.group_id";
    //$check_last_conversation = $wpdb->get_results($ss);
    $response = print 'hello';

So I suppose there is some problems on how I've written my query.

$ss = "select * from ".$wpdb->prefix."vp_pms_messages inner join ".$wpdb->prefix."vp_pms_group_users on ".$wpdb->prefix."vp_pms_messages.id = ".$wpdb->prefix."vp_pms_group_users.message_id and ".$wpdb->prefix."vp_pms_messages.group_id = ".$wpdb->prefix."vp_pms_group_users.group_id where ".$wpdb->prefix."vp_pms_group_users.from_username = '$session_uid' and ".$wpdb->prefix."vp_pms_group_users.from_del = '0' or ".$wpdb->prefix."vp_pms_group_users.to_username = '$session_uid' and ".$wpdb->prefix."vp_pms_group_users.to_del = '0' group by ".$wpdb->prefix."vp_pms_messages.group_id";

Said that, I can't see the error.

  • My apache_error.log and mysql_error_log.err don't report anything about.
  • My tables are empty at now but should they print nothing than produce that error.

Can you please suggest something?

EDIT

I see this error in my console

enter image description here

MySQL table empty

enter image description here

My Wordpress Debug is active like:

enter image description here

My debug.log file (wp-content) is not showing any error in my code.


I've discovered that there is a fatal error in the same file of my query:

PHP Fatal error: Call to undefined function get_bloginfo()

I could check it trough the server php error log. Working on MAMP you can find it here:

MAMP/logs/php_error.log

In my case, Wordpress didn't report it in wp-content/debug.log. So you know. It takes me to the conclusion that my file.php doesn't recognise wordpress hooks and could happen for $wpdb too, throughout my query.


Solution

During the development always turn on the error reporting to be aware of warnings notice or fatal errors which can occur very easily if you forget something or miss place something. So better to be aware or errors and turn the error reporting on to see the errors and when in production do disable the error reporting.

go into wp-config.php file and search for : define('WP_DEBUG',false); and change it to define('WP_DEBUG',true);

Even if your query is okay , you will still result to an error , which you will be getting due to incorrect printing of an array:

function get_results of $wpdb , is an function that will return an array as result of multi rows , and for dumping it use :

print_r($check_last_conversation);

or

var_dump($check_last_conversation); 

print 'hello; works because it is a string , and $check_last_conversation is an array. Don't wrap the print_r or var_dump or print or echo inside an variable. as they are supposed to print the data inside from variables.

so in case you have more errors , you can look at the errors , by turning the error reporting on.



Answered By - Arsh Singh
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing