我想創建一個基於PHP的搜索功能,用下面的代碼:PHP mysql_fetch_array /行不顯示獨特的元素
<?php
$d_name=$_POST['id'];
//connect to the database
$db=mysql_connect ("localhost", "root", "") or die ('I cannot connect to the database because: ' . mysql_error());
//-select the database to use
$mydb=mysql_select_db("pgr");
if(!$mydb)
echo "db not selected";
//-query the database table
$d_name=(int)$d_name;
$sql="SELECT * FROM omimentry WHERE OMIM_ID=$d_name";
//-run the query against the mysql query function
$result=mysql_query($sql) or die(mysql_error());
$n=mysql_fetch_array($result);
//-create while loop and loop through result set
if($n)
{
echo "<table border = 1 width=\"95%\" border=\"0\" cellpadding=\"2\" cellspacing=\"2\" class=\"text_black\">
<tr class=\"yellow\">
<td width=\"10%\" align=\"center\" valign=\"middle\" class=\"text_black_bold\">OMIM_ID</td>
<td width=\"20%\" align=\"center\" valign=\"middle\" class=\"text_black_bold\">location(chromosome)</td>
<td width=\"30%\" align=\"center\" valign=\"middle\" class=\"text_black_bold\">phenotype</td>
<td width=\"20%\" align=\"center\" valign=\"middle\" class=\"text_black_bold\">MIMnumber</td>
<td width=\"20%\" align=\"center\" valign=\"middle\" class=\"text_black_bold\">Gene</td>
<td width=\"20%\" align=\"center\" valign=\"middle\" class=\"text_black_bold\">GeneMIMnumber</td>
<td width=\"20%\" align=\"center\" valign=\"middle\" class=\"text_black_bold\">geneid</td>
<td width=\"30%\" align=\"center\" valign=\"middle\" class=\"text_black_bold\">protein</td>
</tr> ";
while($rows=mysql_fetch_row($result))
{
echo "
<tr class=\"text_black\" align=\"left\" valign=\"middle\">
<td width=\"10%\">$rows[1]</td>
<td width=\"20%\">$rows[2]</td>
<td width=\"30%\">$rows[3]</td>
<td width=\"10%\">$rows[4]</td>
<td width=\"20%\">$rows[5]</td>
<td width=\"20%\">$rows[6]</td>
<td width=\"20%\">$rows[7]</td>
<td width=\"30%\">$rows[8]</td>
</tr>";
}
echo " </table> " ;
}
else
echo "Sorry Data Not Available";
?>
的問題,這是相當獨特的,是這個代碼顯示這些查詢其有重複的條目(甚至對於那些,它顯示最後一行的結果,而不是所有具有重複OMIM_ID的行)。而對於唯一的OMIM_ID,即使沒有明確的錯誤,也沒有結果。
表計劃: INT OMIM_ID VARCHAR2位置(染色體) VARCHAR2表型 INT MIMnumber VARCHAR2基因 INT geneMIMNumber INT geneid VARCHAR2蛋白
什麼我做錯了任何線索?
--Update--
即使更新我的代碼後:
<?php
$d_name=$_POST['id'];
$db = new PDO('mysql:host=localhost;dbname=pgr;charset=utf8', 'root', '');
if(!$mydb)
echo "db not selected";
//-query the database table
$d_name=(int)$d_name;
$stmt = $db->prepare("SELECT * FROM table WHERE id=?");
$stmt->execute(array($d_name));
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
echo "<br>";
echo "<br>";
foreach($rows as $name => $value) {
echo "<tr><th>".htmlspecialchars($name).
"</th><td>".htmlspecialchars($value)."</th></tr>";
?>
我收到同樣的錯誤。
更新:對問題更清晰的看法。
Database:
Table:
int OMIM_ID|varchar2 location(chromosome)|varchar2 phenotype|int MIMnumber|varchar2 Gene|int geneMIMNumber|int geneid|varchar2 Protein
Entries:
2141|24q.xx|Some disease|4651|SomeID|56525|5625|SomeProteinID
2141|21q.xx|Some disease|4651|SomeID|56545|5625|SomeProteinID
2142|24q.xx|Some disease|4651|SomeID|56525|5625|SomeProteinID
。現在,因爲2141作爲查詢,代碼會顯示結果第2項,不是第一個,這應該被列入了。
如果2142是查詢,沒有輸出,理想情況下,可以輸出本來應該由OMIM_ID描述行2142
請,[停止使用'mysql_ *'功能](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php)。他們不再被維護,並[正式棄用](https://wiki.php.net/rfc/mysql_deprecation)。瞭解[準備的陳述](http://en.wikipedia.org/wiki/Prepared_statement),並考慮使用[PDO](http://jayblanchard.net/demystifying_php_pdo.html)。 –
@JayBlanchard,將通過它看,但我只是想知道這個代碼有什麼問題。 – Siddharth
@Siddharth:你的意思是,除了你正在使用一個不推薦的擴展,並且你依賴於mysql隱式回退到最後一個活動連接,並且你正在迴應標記(不要這麼做,認真), **和**你似乎忘記數組是零索引('$ rows [0]'是第一個元素),***和***你很容易注入? –