SQL Query在phpmyAdmin中工作,但在PHP页面上不起作用

时间:2016-04-18 14:54:46

标签: php mysql wordpress

我有一种双重查询,在phpMyAdmin中工作得很完美(试图从Wordpress创建的数据库中获取数据),但无法让它在没有加载任何wordpress功能的独立php页面上工作。在第二个查询193来自$_POST['MAINID']

这是第一个在phpMyAdmin中运行但在PHP中不起作用的查询:

Select distinct wpum.user_id, sum(wppm2.meta_value)
from wp_usermeta wpum
inner join wp_postmeta wppm1 on wpum.user_id = wppm1.meta_value and wppm1.meta_key = 'school_id'
inner join wp_postmeta wppm2 on wppm1.post_id = wppm2.post_id and wppm2.meta_key = 'total_price'    
where Umeta_ID in (select MIN(umeta_id) from wp_usermeta group by USER_ID)    
group by wpum.user_id

然后这是第二个查询结果,但在PHP中不起作用:

select SUM(meta_value) from wp_postmeta
where post_id
in (select distinct post_id from wp_postmeta where meta_value = '193')
and meta_key = 'total_price'

它应该返回331行。但我无法在页面上找到任何工作。我尝试了mysqli_multi_queryMYSQL_ASSOC,甚至没有格式化的简单结果转储。我只想显示用户193的总销售额。

$dbh = new PDO("mysql:host=$hostname;dbname=$dbname", $username, $password); 
$dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION ); //define sql query
$sql = "Select distinct wpum.user_id, sum(wppm2.meta_value) from wp_usermeta wpum inner join wp_postmeta wppm1 on wpum.user_id = wppm1.meta_value and wppm1.meta_key = 'school_id' inner join wp_postmeta wppm2 on wppm1.post_id = wppm2.post_id and wppm2.meta_key = 'total_price' where Umeta_ID in (select MIN(umeta_id) from wp_usermeta group by USER_ID) group by wpum.user_id";

PHP版本为5.2,MySQL版本为5.5.42。

0 个答案:

没有答案