[英]echo inside if else in php
I want to use echo
inside if...else
so i can print "-" if the data doesn't exist in the database, and if the data exist in database, print the data.我想在
if...else
中使用echo
,如果数据库中不存在数据,我可以打印“-”,如果数据库中存在数据,则打印数据。 Until now, my "-" doesn't shows up if the data doesn't exist in database.到目前为止,如果数据库中不存在数据,我的“-”不会出现。
This is for printing on localhost.php这是用于在 localhost.php 上打印
<?php
$kata='';
$no=1;
$data = mysqli_query($koneksi,"SELECT msdosen.`IDDOSEN`, `POSITION`,
`COMPANY`, `START`, `END`
FROM trprofessionalhis
INNER JOIN msdosen ON msdosen.IDDOSEN = trprofessionalhis.IDDOSEN
WHERE trprofessionalhis.IDDOSEN ='$id'
ORDER BY trprofessionalhis.`START` DESC");
while($d = mysqli_fetch_array($data)){
?>
<br><?php echo $d['POSITION'];?><br><?php echo $d['COMPANY'];?>
<br><?php echo $d['START'];?>-<?php echo $d['END']?><br>
<?php
if (empty($d['POSITION'])){
echo '-';
}
else{
echo '';
}
}
?>
I expect the output prints "-" if the data doesn't exist in database.如果数据库中不存在数据,我希望 output 打印“-”。
If your SQL query returns no result, $d
will return NULL
and the while-loop will end before it starts.如果您的 SQL 查询未返回任何结果,则
$d
将返回NULL
并且 while 循环将在开始之前结束。 Therefore none of the echo statements in the loop will be run.因此,循环中的任何 echo 语句都不会运行。
Given the screenshot in this comment , I think this is the case.鉴于此评论中的屏幕截图,我认为情况就是如此。 That is why your if-then never ran in the case intended and shows nothing:
这就是为什么您的 if-then 从未按预期运行并且什么也没显示的原因:
As @catcon mentioned in comment , you can use mysqli_num_row to check if there is any result at all.正如评论中提到的@catcon ,您可以使用mysqli_num_row检查是否有任何结果。 That way you may still show something when there is no result:
这样,当没有结果时,您仍然可以显示一些东西:
<?php
$kata='';
$no=1;
$data = mysqli_query($koneksi,"SELECT msdosen.`IDDOSEN`, `POSITION`,
`COMPANY`, `START`, `END`
FROM trprofessionalhis
INNER JOIN msdosen ON msdosen.IDDOSEN = trprofessionalhis.IDDOSEN
WHERE trprofessionalhis.IDDOSEN ='$id'
ORDER BY trprofessionalhis.`START` DESC");
// add this to print "-" if the query returns no result
if (mysqli_num_row($data) <= 0) {
echo '<br> -';
}
?>
<?php while($d = mysqli_fetch_array($data)) { ?>
<br><?php echo $d['POSITION'];?><br><?php echo $d['COMPANY'];?>
<br><?php echo $d['START'];?>-<?php echo $d['END']?>
<?php } ?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.