2015-04-15 78 views
-2

我不知道PHP編碼。
我設法將一個代碼從MySQL中提取一些數據。
我可以看到HTML表格的結果。PHP - 如何在這種情況下實現IF?

我想不通:
我想在HTML表中添加一個if聲明一樣,if $x1 = ""然後不迴應任何事情,否則返回結果。

這裏是我有:

<?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 = "",將呼應標題:/

+2

你可以使用數據庫,但不知道如何編寫基本的if語句?真? –

+0

@JohnConde,因爲SQL是一種查詢語言,我認爲更多的人會接觸到它,而不是你所期望的。業務分析員,行政助理等。許多辦公室都受到所有強大的電子表格的支配,對於專家用戶來說,跳轉到SQL並不是那麼令人不安。 – DavidS

+0

不要使用'mysql_ *'函數,它們已被棄用。 –

回答

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); 

?> 
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);} 
?> 
+0

我仍然可以看到表頭:/ – James

+0

是這個條件嗎?如果($ x1!=「」 – yesitsme

+0

那麼即使$ x1沒有值,html表格正在創建並且我可以看到標題。 – James

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"; } 

?>