1

Code:

   $Username = $_SESSION['VALID_USER_ID'];

   $q = mysql_query("SELECT * FROM `article_table` 
                      WHERE `Username` = '$Username' 
                      ORDER BY `id` DESC");

   while($db = mysql_fetch_array($q)) { ?>

       <?php if(!isset($db['article'] && $db['subject'])) { 
           echo "Your articles";  
       } else { 
             echo "You have no articles added!"; 
       } ?>    

   <?php } ?>

So I want the rows for example(db['article'] and $db['subject']) from a specific username (see: $Username = $_SESSION['VALID_USER_ID'];) to echo the information if is not empty else if is empty to echo for example "You have no articles added!"

If is some information in the rows the code works, echo the information BUT if the rows is empty don't echo nothing, the code should echo "You have no articles added!" but this line don't appear, where is the mistake?

I tried for if !isset, !empty, !is_null but don't work.

4

6 回答 6

7

我认为你想要实现的是:

$Username = $_SESSION['VALID_USER_ID'];
$q = mysql_query("SELECT * FROM `article_table` WHERE `Username` = '$Username' ORDER BY `id` DESC");

if(mysql_num_rows($q) > 0)
{
    echo "Your articles:\n";
    while($db = mysql_fetch_array($q)) { 
       echo $db['subject']." ".$db['article']."\n";       
    } 
}
else
{
    echo "You have no articles added!";
}

?>
于 2013-08-20T09:34:55.427 回答
2

我不明白。您是否有带有用户名但没有文章的文章行,即:

|   id   |    user   |    article   |
-------------------------------------
|   1    |     X     |      NULL    |

如果是这样,您可以使用以下方法进行测试:

if($db['article'] == NULL) { .... } else { .... }

否则,如果没有user=x的行,当没有记录时,mysql会返回一个空结果。

因此,基本上,如果在 selection: 上找不到任何行SELECT * FROM article_table WHERE Username = 'X';,您可以测试

if(mysql_num_rows($q) > 0) { .... } else { .... }

但是,不再推荐使用 mysql_ 函数。查看准备好的语句。

于 2013-08-20T09:30:28.107 回答
0

我会做这样的事情:

$articles='';
$Username = $_SESSION['VALID_USER_ID'];
$q = mysql_query("SELECT * FROM `article_table` WHERE `Username` = '$Username' ORDER BY     `id` DESC");
while($db = mysql_fetch_array($q)) {

if(isset($db['article']) && isset($db['subject'])) { 
$articles .= $db['article']."<br/>";  
}        

} 


if($articles != ''){
echo $articles;
}
else{
echo "No articles";
}


?>
于 2013-08-20T09:31:40.360 回答
0

你的if陈述中有一个逻辑错误——你想要检查文章和主题是否都设置了。

使用您当前的代码,与 进行比较$db['article']$db['subject']并检查结果是否已设置。你需要稍微改变一下:

代替 :

if(!isset($db['article'] && $db['subject'])) { 

尝试:

if(isset($db['article']) && isset($db['subject'])) ...
于 2013-08-20T09:30:10.747 回答
0

实现您想要的最快方法是添加一个变量,该变量将验证查询是否返回任何行:

<?php         $Username = $_SESSION['VALID_USER_ID'];
  $i = 0;
  $q = mysql_query("SELECT * FROM `article_table` WHERE `Username` = '$Username' ORDER BY `id` DESC");
  while($db = mysql_fetch_array($q)) {
    $i = 1;
    if(!isset($db['article'] && $db['subject'])) { echo "Your articles";  } ?>        
    <?php } 
    if ($i == 0) echo "You have no articles";
?>

您试图在 while 循环中回显“无文章”,只有当查询返回信息时才会到达那里,这就是为什么如果它返回 1 行或更多行,$i 将变为 1,否则它将保持为 0。

于 2013-08-20T09:34:04.073 回答
-1

在你的情况下:

$numArticles = mysql_num_rows($q);

if($numArticles > 0)
 echo 'Your articles';
else
 echo 'No articles :((((';

我建议艰难地转向 PDO 以与 DB 进行通信。

于 2013-08-20T09:33:14.397 回答