我不是程序员,我正在尝试学习一些编码,我使用一些相同的代码搜索MySQL并在页面中显示结果,如果PHP代码在mysql中找不到值,则不会显示任何结果。我想在这种情况下显示我的自定义消息,如果mysql中没有'serialno'值,我需要显示“ xxx” msg
我的代码是这个
<?php header('Content-type: text/html; charset=utf-8'); ?>
<?php
$db_hostname = 'localhost';
$db_username = 'XXX';
$db_password = 'XXX';
$db_database = 'XXX';
// Database Connection String
$con = mysqli_connect($db_hostname,$db_username,$db_password);
mysqli_query ($con, "set character_set_client='utf8'");
mysqli_query ($con, "set character_set_results='utf8'");
// mysqli_query ("set collation_connection='utf8_general_ci'");
if (!$con)
{
die('Could not connect: ' . mysqli_error());
}
mysqli_select_db($con, $db_database);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>جستجوی شماره سریال</title>
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<form class="form-wrapper cf" action="" method="post">
<div class="byline"><p style="direction: rtl">شماره سریال دستگاه را وارد کنید :</p></div>
<input type="text" name="term" />
<button type="submit">جستجو</button>
</form>
</body>
<?php
if (!empty($_REQUEST['term'])) {
$term = mysqli_real_escape_string($con, $_REQUEST['term']);
$sql = "SELECT * FROM serial WHERE serialno = ".$term;
$r_query = mysqli_query($con, $sql);
while ($row = mysqli_fetch_assoc($r_query)){
/* echo '<br /> ID: ' .$row['id']; */
echo '<div style="direction: rtl"><font size="12"'." face='Tahoma'>";
echo '<br /> شماره سریال: '.$row['serialno'];
echo '<br />تامین کننده: '.$row['vendor'];
echo '<br /> وضعیت: '.$row['stat'];
echo '</div>';
}
}
?>
</body>
<br>
<br>
<br>
<br>
<br>
<br>
</html>
答案 0 :(得分:0)
您可以使用mysqli_num_rows(query);它将返回查询返回的行数,然后编写一个if函数来显示消息。
if (!empty($_POST['term'])) {
$term = mysqli_real_escape_string($con, $_POST['term']);
$sql = "SELECT * FROM serial WHERE serialno = ".$term;
$r_query = mysqli_query($con, $sql);
$num_rows = mysqli_num_rows($r_query); //Number of rows query returned
if($num_rows > 0) { //Here you check if that number is higher then 0 you go with the loop
while ($row = mysqli_fetch_assoc($r_query)){
/* echo '<br /> ID: ' .$row['id']; */
echo '<div style="direction: rtl"><font size="12"'." face='Tahoma'>";
echo '<br /> شماره سریال: '.$row['serialno'];
echo '<br />تامین کننده: '.$row['vendor'];
echo '<br /> وضعیت: '.$row['stat'];
echo '</div>';
}
} else {
//Here you display your message
}
这对SQL注入攻击也是开放的,因此PDO是更好的选择。
答案 1 :(得分:0)
如果我对您的理解正确,那么您想在“ serialno”没有价值时向用户显示一条消息吗?
while ($row = mysqli_fetch_assoc($r_query)){
if(isset($row['serialno'])){
/* echo '<br /> ID: ' .$row['id']; */
echo '<div style="direction: rtl"><font size="12"'." face='Tahoma'>";
echo '<br /> شماره سریال: '.$row['serialno'];
echo '<br />تامین کننده: '.$row['vendor'];
echo '<br /> وضعیت: '.$row['stat'];
echo '</div>';
}else{
echo "No value in serialno";
}
}
或者如果您仍然想显示其余的行:
while ($row = mysqli_fetch_assoc($r_query)){
/* echo '<br /> ID: ' .$row['id']; */
echo '<div style="direction: rtl"><font size="12"'." face='Tahoma'>";
echo '<br /> شماره سریال: '.(isset($row['serialno'])?$row['serialno']:"Row has serialno");
echo '<br />تامین کننده: '.$row['vendor'];
echo '<br /> وضعیت: '.$row['stat'];
echo '</div>';
}
但是您应该真正使用PDO;)
答案 2 :(得分:0)
<?php
if (!empty($_REQUEST['term'])) {
$term = mysqli_real_escape_string($con, $_REQUEST['term']);
$sql = "SELECT * FROM serial WHERE serialno = ".$term;
$r_query = mysqli_query($con, $sql);
if( mysqli_num_rows($r_query) > 0 )
{
while ($row = mysqli_fetch_assoc($r_query)){
/* echo '<br /> ID: ' .$row['id']; */
echo '<div style="direction: rtl"><font size="12"'." face='Tahoma'>";
echo '<br /> شماره سریال: '.$row['serialno'];
echo '<br />تامین کننده: '.$row['vendor'];
echo '<br /> وضعیت: '.$row['stat'];
echo '</div>';
}
}
else{
echo "<p>YOUR CUSTOM MESSAGE</p>";
}
}
?>
答案 3 :(得分:-1)
最好使用PDO enter link description here
用这种方式会遇到很多问题