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

*爱你&永不变心* 提交于 2019-12-13 03:59:32

问题


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

MySQL table empty

My Wordpress Debug is active like:

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.


回答1:


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.




回答2:


As OP mentioned that he is working on external wordpress script and is not able to access his wordpress functionality. To ensure that you retain your wordpress functionality on your external files, please put the following code, at the start of your external script page.

$path = $_SERVER['DOCUMENT_ROOT']; 
include_once $path . '/wp-load.php';

The above lines, will include all the wordpress functionality for your script. $path variable stores the path of your wordpress installation folder.



来源:https://stackoverflow.com/questions/38786757/wordpress-500-internal-server-error-probable-issue-using-wpdb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!