如果列为空则显示echo,否则显示另一个echo

时间:2017-03-11 02:18:31

标签: php

我编写了这段代码,但是有一些错误,即使列不为空,它也只会显示第一个回显。

如果列subuser1为空,我会将代码显示为echo“1” 如果列subuser1不为空,则显示echo“2”。

  <?php

  include_once 'dbconnect.php';

   // Create connection
  $conn = new mysqli(DBHOST,DBUSER,DBPASS,DBNAME);
// Check connection
if ($conn->connect_error) {
 die("Connection failed: " . $conn->connect_error);
}  

$sql= ("SELECT subuser1 FROM users WHERE id=".$_SESSION['user']  );
$result = $conn->query($sql);


$subuser1 = $row["subuser1"];


if ($result->num_rows > 0) {

if (empty($subuser1)) {

echo "subuser1 is either 0, empty, or not set at all";
}
 } else {
echo " subuser1 not empty";

1 个答案:

答案 0 :(得分:0)

我解决它就像任何人都需要它一样完美。

 <?php
 $userR= 0 ;
 include_once 'dbconnect.php';

 // Create connection
 $conn = new mysqli(DBHOST,DBUSER,DBPASS,DBNAME);
 // Check connection
if ($conn->connect_error) {
 die("Connection failed: " . $conn->connect_error);
}  

$sql= ("SELECT * FROM users WHERE id=".$_SESSION['user']  );
$result = $conn->query($sql);

if ($result->num_rows > 0) {

 while($row = $result->fetch_assoc()) {  

  if ($row["subuser1"] == NULL ){
 echo "";  }
   else {

echo " bla bla bla "
}
}
?>