我想创建一个基于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]'是第一个元素),***和***你很容易注入? –