我從數據庫中檢索3個字段,默認情況下,只有用戶名字段將包含內容。其他信息將從用戶應用程序中添加。如果該字段中沒有條目,我想將該字段顯示爲「無信息」。我試圖使用empty()和is_null()都無濟於事。在兩種情況下,$ row ['firstname']和$ row ['lastname']的var_dump都返回NULL。檢查數據庫中的空值 - 失敗
<?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);
}
}
這兩種方法沒有什麼區別。埃。 – 2009-04-20 13:50:39