简体   繁体   中英

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!"

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.

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.

So, basicly, if no rows are found on selection: SELECT * FROM article_table WHERE Username = 'X'; , you can test

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

However, mysql_ functions are not recommended anymore. 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.

With your current code, you compare $db['article'] with $db['subject'] , and check if the result is set. 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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