Check null value from database - crash

I am extracting 3 fields from the database, and by default only the username will contain the content. Other information will be added from the user application. I want to show the "No Information" field if there is no entry in the field. I am trying to use both empty () and is_null () to no avail. 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);
    }
}
+3
source share
4 answers

Turn on display_errors and set error_reporting to E_ALL (either in php.ini or in htaccess). This should reveal any unobvious errors.

Try the following: check if something is empty:

<?php
if(!isset($row['...']) || empty($row['...']))
{
    $row['firstname'] = 'No Info';
}
?>

, , '==', $row " " . Al return 'true' ( 1). '=' .

+4

:

$row ['firstname'] == "No Info"; $row ['firstname'] = " ";

:

  • ? ? , !
  • include.
  • , ?
+3

, ...

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

if(isset($row['field'])) { }
+1

You can check for empty data using the is_null property . Refer to 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
}
0
source

Source: https://habr.com/ru/post/1706724/


All Articles