简体   繁体   中英

How to count row in database mysql and get the value, if null value = 0

I want to insert image to Database but I want to check if there is already stored image or not. If no image before, update the database, if there is image in database, update database and unlink prev image.

$stmt = $db->query("SELECT COUNT(*) FROM pr_journalist WHERE id='".$id."' AND foto='' ");
    
    
    if($stmt = 0) {
    
        // Insert image file name into database
            $insert = $db->query("UPDATE pr_journalist SET foto='".$fileName."' WHERE id='".$id."'");
        
                        
        unlink("uploads/".$foto);
           $statusMsg = 'ok';
       
        }else{
        // Insert image file name into database
            $insert = $db->query("UPDATE pr_journalist SET foto='".$fileName."' WHERE id='".$id."'");
       
                        
           $statusMsg = 'ok';
        
    }

With my above code, counting Row always make me to one if condition, although im trying to change to foto IS NOT NULL and change if condition to.= 0 or foto IS NULL with if = 0. It is like if ($stmt = 0) always got the same value from row count.

To count rows you should not do $stmt = 0,

its,

$count=query->rowCount();

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