Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL select rows and ignore row if column is blank

Using Dreamweaver CS5 / MySQL

I'm trying to set up a query that will display rows of data but only if a specific column has data in it. If the Charname column is blank I don't want that row to be displayed.

Currently the query works, but it also displays the rows when the Charname column is empty. I'm struggling to set up and have failed at every turn to do it. Thanks in advance.

mysql_select_db($database_login_form, $login_form);
$query_Recordset1 = "SELECT * FROM users";
$Recordset1 = mysql_query($query_Recordset1, $login_form) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);


<?php do { ?>
<li><a href="profile.php?id=<?php echo $row_Recordset1['id']; ?>"><?php echo $row_Recordset1['Charname']; ?></a></li>
<?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>
like image 429
Cyndi Avatar asked Jan 30 '23 23:01

Cyndi


2 Answers

You could use COALESCE() to treat blank (empty string) and NULL as the same thing:

SELECT * FROM users WHERE COALESCE(Charname, '') != ''

You never told us what "empty" actually means, but a good guess would be empty string or NULL.

like image 175
Tim Biegeleisen Avatar answered Feb 02 '23 16:02

Tim Biegeleisen


update your query like this

SELECT * FROM users where Charname IS NOT NULL or Charname != '';

you can also use COALESCE() for check null

SELECT * FROM users WHERE COALESCE(Charname, '') != '';

for more information

https://www.w3schools.com/sql/sql_isnull.asp

and you can aslo check in your php end

suppose like this

$Charname = $row_Recordset1['Charname'];
if($Charname == '' || $Charname === null || is_null($Charname)){
   continue;
}
like image 38
Shafiqul Islam Avatar answered Feb 02 '23 18:02

Shafiqul Islam