0
我使用php和數據庫製作了一個英文波斯語詞典,我從用戶那裏獲取英文單詞並將其發佈到process.php,然後在該文件中我在我的數據庫中搜索輸入的單詞,並返回波斯語意義。這工作正常,但我的問題是,當輸入的單詞不在我的數據庫中時,代碼不會進入else條件,並且不會打印「0結果」聲明。如果有人能幫忙,我會非常感謝。 所以這是我的第一個文件:什麼是數據庫搜索中找不到的數據的其他條件
<html>
<head>
<style>
body {
background-image: url("final.jpg");
}
#par {
width: 320px;
padding: 10px;
border: 5px solid gray;
margin-left:auto;
margin-Right:auto;
position: absolute;
top: 20%;
left:35%;
text-align: center;
background-color:Powderblue;
}
#footer{
margin-top:45%;
background-color:#C7BDBB;
text-align:right;
}
</style>
<title>niloofar-dictionary</title>
</head>
<body>
<div id=par>
<?php
$username="raanaste_niloo1";
$password="Nt13541372";
$dbname="raanaste_niloofar-dictionary";
$usertable="dictionary";
$yourfield = "english";
$yourfield1 = "persian";
//Connect to the database
$connection = mysql_connect($hostname, $username, $password);
mysql_select_db($dbname, $connection);
$name = $_POST["word"];
//Setup our query
$query = "SELECT persian FROM $usertable WHERE english='{$_POST["word"]}'";
//Run the Query
$result = mysql_query($query);
//If the query returned results, loop through
// each result
if($name)
{
if($result!=NULL)
{
while($row = mysql_fetch_array($result))
{
$na = $row["$yourfield1"];
echo "word in persian: " . $na;
}}
else {
echo "0 results"
}
}
?>
</div>
<div id="footer">
<h4> COPYRIGHT: © 2017 niloofartarighat. </h4></div>
</body>
</html>
,這是process.php
[小博](http://bobby-tables.com/)說***腳本是在對SQL注入攻擊的風險。(http://stackoverflow.com/問題/ 60174 /何燦I-防止-SQL注入式-PHP)***。即使[轉義字符串](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string)是不安全的! –
***請[停止使用'mysql_ *'功能](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php)。*** [這些擴展名](http://php.net/manual/en/migration70.removed-exts-sapis.php)已在PHP 7中刪除。瞭解[prepared](http://en.wikipedia.org/wiki/Prepared_statement )[PDO]聲明(http://php.net/manual/en/pdo.prepared-statements.php)和[MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared- statement.php),並考慮使用PDO,[這真的很簡單](http://jayblanchard.net/demystifying_php_pdo.html)。 –
任何地方你都沒有'else'條件。 –