Reputation: 4193
I am retrieving 3 fields from a database, and by default, only the username field will have content. The other information will be added in from a user application. I want to show the field as "No Info" if there is no entry in the field. I am trying to use both empty() and is_null() to no avail. A var_dump of $row['firstname'] and $row['lastname'] returns NULL in both cases.
<?php
if (isset($_GET["subcat"]))
$subcat = $_GET["subcat"];
if (isset($_GET["searchstring"])) {
$searchstring = $_GET["searchstring"];
}
$con = mysqli_connect("localhost","user","password", "database");
if (!$con) {
echo "Can't connect to MySQL Server. Errorcode: %s\n". mysqli_connect_error();
exit;
}
$table = 'USERS';
$brand = '%' . $searchstring . '%';
$rows = getRowsByArticleSearch($brand, $table);
echo "<table border='0' width='100%'><tr>" . "\n";
echo "<td width='15%'>Username</td>" . "\n";
echo "<td width='15%'>Firstname</td>" . "\n";
echo "<td width='15%'>Surname</td>" . "\n";
echo "</tr>\n";
foreach ($rows as $row) {
if (is_null($row['firstname']) || $row['firstname'] == "") {
$row['firstname'] == "No Info";
}
if ($row['lastname'] == null || $row['lastname'] == "") {
$row['firstname'] == "No Info";
}
var_dump($row['firstname']);
var_dump($row['lastname']);
echo '<tr>' . "\n";
echo '<td><a href="#" onclick="updateByUser(\''. $row['username'] .'\',\''.$subcat.'\')">'.$row['username'].'</a></td>' . "\n";
echo '<td><a href="#" onclick="updateByUser(\''. $row['username'] .'\', \''.$subcat.'\')">'.$row['firstname'].'</a></td>' . "\n";
echo '<td><a href="#" onclick="updateByUser(\''. $row['username'] .'\', \''.$subcat.'\')">'.$row['lastname'].'</a></td>' . "\n";
echo '</tr>' . "\n";
}
echo "</table>\n";
function getRowsByArticleSearch($searchString, $table) {
$con = mysqli_connect("localhost","user","password", "database");
$recordsQuery = "SELECT username, firstname, lastname FROM $table WHERE lower(username) LIKE ? ";
if ($getRecords = $con->prepare($recordsQuery)) {
$getRecords->bind_param("s", $searchString);
$getRecords->execute();
$getRecords->bind_result($username, $firstname, $lastname);
$rows = array();
while ($getRecords->fetch()) {
$row = array(
'username' => $username,
'firstname' => $firstname,
'lastname' => $lastname,
);
$rows[] = $row;
}
return $rows;
} else {
print_r($con->error);
}
}
Upvotes: 3
Views: 10523
Reputation: 577
You can check the empty data using is_null property .Refer this link for more details http://php.net/manual/en/function.is-null.php
is_null($row['field'])
Use
if(is_null($rows['field'])){
// Code Here
}else{
//code here
}
Upvotes: 0
Reputation: 40675
First and your main issue:
You are using comparison operators in $row['firstname'] == "No Info"; instead of assignement operators $row['firstname'] = "No Info";
Other issues:
Upvotes: 3
Reputation: 11507
Turn on display_errors and set error_reporting to E_ALL (either in php.ini or in a htaccess). This should reveal any non-obvious mistakes.
Try the following for checking if something is empty:
<?php
if(!isset($row['...']) || empty($row['...']))
{
$row['firstname'] = 'No Info';
}
?>
Also, please note you're using '==' to set the $row things to 'No Info'. Al this does is return 'true' (or 1). Use a single '=' to set variables.
Upvotes: 5
Reputation: 180004
Two things to try...
if($row['field'] === NULL) { }
if(isset($row['field'])) { }
Upvotes: 1