tags:

views:

164

answers:

3

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);
    }
}
+1  A: 

Two things to try...

if($row['field'] === NULL) { }

if(isset($row['field'])) { }
ceejayoz
both of these methods made no difference. Aye.
Joshxtothe4
+3  A: 

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:

  • Why do you establish the connection twice? Or actually multiple times? Find a way to let your functions know the connection!
  • Factor the connection out into an include.
  • Why do you do that while loop, if you can access the returned array directly?
tharkun
+1  A: 

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.

Bart S.