我不知道PHP
编码
我设法将一些代码放在一起,从MySQL
中提取一些数据
我可以将结果看作HTML表格。
我无法弄清楚:
我想添加一个if
语句,如if $x1 = ""
然后不回显任何内容,否则返回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 = ""
时,会回显标题:/
答案 0 :(得分:0)
$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);}
?>
答案 1 :(得分:0)
用以下代码替换您的代码:
<?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"; }
?>
答案 2 :(得分:0)
好的,我是如何解决的。
//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);
?>