我为我的WP网站创建了一个简单的脚本,我正在尝试从2个表的WP数据库中获取正确的数据,这是代码,
im显示" user_ref_id"列的结果时,正在重复多次这是代码
global $wpdb;
$tableuser=$wpdb->prefix.'user_shah';
$tablecopn=$wpdb->prefix.'user_coupons_shah';
$results_count = $wpdb->get_results("SELECT User_Status, Coupon_Status, User_ref_id FROM $tableuser t1 INNER JOIN $tablecopn t2 ON t1.User_Status=t2.Coupon_Status WHERE t1.User_Status='1'");
$res_countus = count($results_count);
if($res_countus>0)
{
echo "<p><select name='autoref' id='myAutoref' onchange='myRefauto()'>
<option>Select Reference</option>";
foreach($results_count as $results2)
{ echo "<option value='{$results2->User_ref_id}'>{$results2->User_ref_id}</option>";
}
echo "</select></p>";
}
您需要将子句添加到MySQL查询。
$results_count = $wpdb->get_results("
SELECT User_Status, Coupon_Status,User_ref_id
FROM $tableuser t1 INNER JOIN $tablecopn t2
ON t1.User_Status=t2.Coupon_Status
WHERE t1.User_Status='1'
GROUP BY User_ref_id ");