使用null选择值为的列

时间:2016-03-16 06:34:31

标签: php mysql

我有一个查询来选择非空值的列,但它不起作用..请告诉我问题出在哪里。谢谢

$query= mysql_query("SELECT opt_one, opt_two, opt_three, opt_four, opt_five, opt_six FROM `table`
        WHERE `opt_one` IS NOT NULL AND `opt_two` IS NOT NULL AND `opt_three` IS NOT NULL AND
        `opt_four` IS NOT NULL AND `opt_five` IS NOT NULL AND `opt_six` IS NOT NULL AND  q_id = '$id'")or die(mysql_error);
while($r3 = mysql_fetch_array($query))
{
   $print = array($r3['opt_one'], $r3['opt_two'], 
   $r3['opt_three'],          
   $r3['opt_four'], $r3['opt_five'], $r3['opt_six']);
   shuffle($print);

   echo $print['0'];
   echo '<br>';
   echo $print['1'];
   echo '<br>';
   echo $print['2'];
   echo '<br>';
   echo $print['3'];
   echo '<br>';
   echo $print['4'];
   echo '<br>';
   echo $print['5'];
   echo '<br>';

}

1 个答案:

答案 0 :(得分:0)

我认为您的列中的值为空 NULL 。试试这个:

SELECT opt_one, opt_two, opt_three, opt_four, opt_five, opt_six 
FROM `table`
WHERE (`opt_one` IS NOT NULL OR `opt_one` != '') 
AND (`opt_two` IS NOT NULL OR `opt_two` != '') 
AND (`opt_three` IS NOT NULL OR `opt_three` != '')
AND (`opt_four` IS NOT NULL OR `opt_four` != '') 
AND (`opt_five` IS NOT NULL OR `opt_five` != '')
AND (`opt_six` IS NOT NULL OR `opt_six` != '')
AND  q_id = '$id'

旁注:

停止使用mysql_ *扩展,在PHP 7中弃用并关闭,使用mysqli_*PDO