PHP MySQL 查询 - 仅返回当前用户的结果 [英] PHP MySQL query - return results for current user only
问题描述
我目前正在使用 wordpress 构建一个网站,该网站上有一个个性测验.有一个结果页面可以检索用户上次提交的值.我已经设法让它返回最后一次提交,但无法弄清楚如何只返回当前用户的最后一次提交.我认为它与 get_currentuserinfo();
和 AND user_id = %d", $form_id, $user_ID
有关系,我发现了 get_currentuserinfo();
在这里:http://codex.wordpress.org/Function_Reference/get_currentuserinfo
I'm currently building a website using wordpress that has a personality quiz on it. There is a results page that retrieves the value of the users last submission. I have managed to get it to return the last submission but cant work out how to only return the current users last submission. I think it has something to do with get_currentuserinfo();
and AND user_id = %d", $form_id, $user_ID
from what I can work out. I discovered get_currentuserinfo();
here: http://codex.wordpress.org/Function_Reference/get_currentuserinfo
你可以在下面看到我的编码.如果您需要更多信息,请告诉我.
You can see my coding below. Please let me know if you need any further information.
<?php
global $wpdb, $ipt_fsqm_info, $user_ID;
get_currentuserinfo();
$form_id = 9;
$data_ids = $wpdb->get_col( $wpdb->prepare( "SELECT id
FROM {$ipt_fsqm_info['data_table']}
WHERE form_id = %d
ORDER BY id DESC LIMIT 0,1
AND user_id = %d", $form_id, $user_ID ) );
foreach ( $data_ids as $data_id ) {
$data = new IPT_FSQM_Form_Elements_Data( $data_id );
echo wpautop( $data->data->pinfo[14]['value'] );
}
?>
推荐答案
您需要移动 AND user_id = %d
$data_ids = $wpdb->get_col( $wpdb->prepare
( "SELECT id FROM {$ipt_fsqm_info['data_table']}
WHERE form_id = %d AND user_id = %d
ORDER BY id DESC LIMIT 0,1", $form_id, $user_ID ) );
这篇关于PHP MySQL 查询 - 仅返回当前用户的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!