为什么这是PHP;在“U数组”中;不使用SQL查询?

为什么这是PHP;在“U数组”中;不使用SQL查询?,php,sql,wordpress,Php,Sql,Wordpress,你好,我有一些wordpress帖子,上面有一个自定义字段,标题是“激励ID”。我有一个php/sql查询,它返回一个逗号分隔的“推荐”激励ID列表 如果您能在这个问题上提供任何帮助,我们将不胜感激 GROUP_CONCAT()将生成一个字符串,因此您的DB结果如下所示 $rows = array( '0' => array('foo' => '1,2,3,4'), '1' => array('bar' => '5,6,7,8') ); 实际上,您正试图在数

你好,我有一些wordpress帖子,上面有一个自定义字段,标题是“激励ID”。我有一个php/sql查询,它返回一个逗号分隔的“推荐”激励ID列表

如果您能在这个问题上提供任何帮助,我们将不胜感激

GROUP_CONCAT()
将生成一个字符串,因此您的DB结果如下所示

$rows = array(
   '0' => array('foo' => '1,2,3,4'),
   '1' => array('bar' => '5,6,7,8')
);

实际上,您正试图在数组(3,'1,2,3,4')中执行
。由于
1,2,3,4
是一个字符串,因此您会收到警告。您需要
explode()
该字符串,然后对从explode()获得的数组执行in_数组。或者不要在数据库中执行
group\u concat
,而是将单个记录提取到while/fetch循环中构建的数组中。

首先从
SELECT
语句中删除
group\u concat()
然后在
SELECT
语句中添加一个
groupby
。 下面是
SELECT
语句的代码,并将其转换为数组

global $wpdb;
$user_ID = get_current_user_id();
$sql = <<<SQL
SELECT T_L_INCENTIVES_ID
FROM incentive_recommendations_v 
WHERE accept_recommendation = 1 AND user_id = $user_ID
GROUP BY T_L_INCENTIVES_ID
SQL;

if (!$sql) { // add this check.
    die('Invalid query: ' . mysql_error());
}

$resultset = array();
$rebates = $wpdb->get_results( $sql, ARRAY_A );
foreach($rebates as $data) {
     $resultset[] = $data['T_L_INCENTIVES_ID'];
}
global$wpdb;
$user\u ID=get\u current\u user\u ID();

$sql=查询中没有GROUPBY子句,您希望查询做什么?另外,如果(!sql){…}
什么也不做,那么
变量
$sql
总是在那里定义的,但此时还没有运行任何查询。
in_array expects parameter 2 to be array, string given.  
$rows = array(
   '0' => array('foo' => '1,2,3,4'),
   '1' => array('bar' => '5,6,7,8')
);
global $wpdb;
$user_ID = get_current_user_id();
$sql = <<<SQL
SELECT T_L_INCENTIVES_ID
FROM incentive_recommendations_v 
WHERE accept_recommendation = 1 AND user_id = $user_ID
GROUP BY T_L_INCENTIVES_ID
SQL;

if (!$sql) { // add this check.
    die('Invalid query: ' . mysql_error());
}

$resultset = array();
$rebates = $wpdb->get_results( $sql, ARRAY_A );
foreach($rebates as $data) {
     $resultset[] = $data['T_L_INCENTIVES_ID'];
}
<?php $incent = get_post_meta(get_the_ID(), 'Incentive ID', true); if ( in_array($incent,$resultset)) { 
          echo'style="background-image:url(/images/gold_medal.jpg); background-repeat:no-repeat; background-size:10% auto; background-position:right top; border:2px solid #ccc; padding:20px 48px 20px 20px; height:auto; padding-bottom:20px; min-height:260px !important;"';
          } else { 
          echo 'style="border:2px solid #ccc; padding:20px 48px 20px 20px; height:auto; padding-bottom:20px; min-height:260px !important;"';}  ?>