简体   繁体   English

PHP MySQL的如果行为空,如果不为空

[英]php mysql if row is empty and if isn't empty

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!" 因此,我希望来自特定用户名(例如: $Username = $_SESSION['VALID_USER_ID']; )的行(例如db['article']$db['subject'] )在不为空的情况$Username = $_SESSION['VALID_USER_ID'];显信息否则,如果为空则回显,例如“您没有添加文章!”

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. 我尝试过!isset!empty!is_null但是不起作用。

I think what you're trying to achieve is: 我认为您要实现的目标是:

$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!";
}

?>

I don't understand. 我不明白 Do you have article rows with username, but without article, ie: 您是否有带有用户名但没有文章的文章行,即:

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

If so, you can test with: 如果是这样,您可以使用以下方法进行测试:

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

Otherwise, if you don't have a row with user=x, when there are no record, mysql will return an empty result. 否则,如果没有user = x的行,则在没有记录时,mysql将返回空结果。

So, basicly, if no rows are found on selection: SELECT * FROM article_table WHERE Username = 'X'; 因此,基本上,如果在选择中未找到任何行: SELECT * FROM article_table WHERE Username = 'X'; , you can test ,您可以测试

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

However, mysql_ functions are not recommended anymore. 但是,不再建议使用mysql_函数。 Look at prepared statements. 查看准备好的语句。

You have a logic error in your if statement -- what you want is to check if both the article and subject are set. if语句中存在逻辑错误-您要检查是否同时设置了文章和主题。

With your current code, you compare $db['article'] with $db['subject'] , and check if the result is set. 使用当前代码,将$db['article']$db['subject'] ,并检查结果是否已设置。 You need to change it a bit : 您需要对其进行一些更改:

Instead of : 代替 :

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

Try: 尝试:

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

I would do something like this: 我会做这样的事情:

$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";
}


?>

fastest way to achieve what you want is by adding a variable that will verify if the query returned any rows: 实现所需目标的最快方法是添加一个变量,该变量将验证查询是否返回了任何行:

<?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";
?>

You tried to echo "no articles" in the while loop, you get there only if the query returns information, that is why if it returns 1 or more rows, $i will become 1 else it will remain 0. 您试图在while循环中回显“没有文章”,只有在查询返回信息时,您才能到达那里,这就是为什么如果它返回1行或更多行,$ i将变为1,否则将保持为0。

In your case: 在您的情况下:

$numArticles = mysql_num_rows($q);

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

I recommend tough moving on to PDO to communicate with DB. 我建议您继续使用PDO与数据库进行通信。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM