PHP-在这种情况下如何实现IF?

PHP-在这种情况下如何实现IF?,php,Php,我对PHP编码一无所知。 我设法编写了一段代码,从MySQL中提取一些数据。 我可以将结果显示为HTML表格 我不明白的是: 我想添加一个if语句,比如if$x1=then不回显任何内容,否则在html表中返回结果 以下是我所拥有的: <?php $x1 = get_field(test); $username = "xxxxx"; $password = "xxxxxx"; $hostname = "localhost"; //connection to the database

我对PHP编码一无所知。 我设法编写了一段代码,从MySQL中提取一些数据。 我可以将结果显示为HTML表格

我不明白的是: 我想添加一个if语句,比如if$x1=then不回显任何内容,否则在html表中返回结果

以下是我所拥有的:

<?php 
$x1 = get_field(test);

$username = "xxxxx";
$password = "xxxxxx";
$hostname = "localhost"; 

//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password) 
 or die("Unable to connect to MySQL");

// select a database to work with
$selected = mysql_select_db("xxxx",$dbhandle) 
  or die("Could not select examples");

// execute the SQL query and return records
$result = mysql_query("SELECT * FROM xxx WHERE xxxx = '$x1'");

// fetch tha data from the database
echo "<table><tr><th>Name</th><th>Nick Name</th><th>Email</th></tr>";

while ($row = mysql_fetch_array($result)) {
   echo 
"<tr><td>".$row["name_l"]."</td><td>".$row["nick_name"]."</td><td>".$row["email_s"]."</td></tr>";
}
echo "</table>";

//close the connection
mysql_close($dbhandle);

上面代码的问题是,当$x1=,将回显标题://

用以下内容替换代码:

$x1 = get_field(test);

if($x1!=""){
$username = "xxxxx";
$password = "xxxxxx";
$hostname = "localhost"; 

//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password) 
 or die("Unable to connect to MySQL");

//select a database to work with
$selected = mysql_select_db("xxxx",$dbhandle) 
  or die("Could not select examples");

//execute the SQL query and return records
$result = mysql_query("SELECT * FROM xxx WHERE xxxx = '$x1'");

//fetch tha data from the database

echo "<table>
<tr><th>Name</th><th>Nick Name</th><th>Email</th></tr>";
while ($row = mysql_fetch_array($result)) {
   echo 
"<tr><td>".$row["name_l"]."</td><td>".$row["nick_name"]."</td><td>".$row["email_s"]."</td></tr>";
}
echo "</table>";

//close the connection
mysql_close($dbhandle);}
?>
<?php 
$x1 = get_field("test");
if($x1){

$username = "xxxxx";
$password = "xxxxxx";
$hostname = "localhost"; 

//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password) 
or die("Unable to connect to MySQL");

//select a database to work with
$selected = mysql_select_db("xxxx",$dbhandle) 
or die("Could not select examples");

//execute the SQL query and return records
$result = mysql_query("SELECT * FROM xxx WHERE xxxx = '$x1'");

//fetch tha data from the database

echo "<table>
<tr><th>Name</th><th>Nick Name</th><th>Email</th></tr>";
while ($row = mysql_fetch_array($result)) {
echo 
"<tr><td>".$row["name_l"]."</td><td>".$row["nick_name"]."   </td><td>".$row["email_s"]."</td></tr>";
}
echo "</table>";

//close the connection
mysql_close($dbhandle);
} 
else {echo "Sorry, Nothing to display"; }

?>

好的,下面是我如何修复它的


您可以使用数据库,但不知道如何编写基本if语句?真的吗?@johncode,因为SQL是一种查询语言,我认为接触它的人比你想象的要多。业务分析师、行政助理等。许多办公室都由全能的电子表格管理,对于专家用户来说,跳转到SQL并没有那么刺耳。不要使用mysql_*函数,它们已被弃用。我仍然可以看到表头:/n这是条件吗?如果$x1=即使$x1没有值,html表也在创建中,我可以看到标题;然后回波测试$x1;
//fetch tha data from the database
if ($x1 == '') {
echo "<table>
<tr><th>Name</th><th>Nick Name</th><th>Email</th></tr>";
while ($row = mysql_fetch_array($result)) {
   echo 
"<tr><td>".$row["name_l"]."</td><td>".$row["nick_name"]."</td><td>".$row["email_s"]."</td></tr>";
}
echo "</table>";} else {}

//close the connection
mysql_close($dbhandle);

?>