PHP mysql_fetch_array / Row不显示唯一元素

时间:2015-05-04 18:07:34

标签: php mysql

我正在尝试使用以下代码创建基于php的搜索功能:

<?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蛋白

关于我做错了什么的线索?

- 更新 -

即使将我的代码更新为:

<?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作为查询,代码将显示结果中的第二个条目,而不是第一个条目,它应该也包括在内。

如果查询是2142,则没有输出,理想情况下,可能的输出应该是OMIM_ID 2142描述的行。

1 个答案:

答案 0 :(得分:1)

I do not know what is in your tables and what duplicates you are referring to. Because you got trashed so bad, I risked getting trashed helping you with unconventional code.

The comments about using depreciated mysql had no bearing on your code but they just don't like others that come here for a solution to think it is OK to use mysql. They do not realize when you post a question how frustrated you are and how they are adding to your frustration. They mean well but in your case you changed your code to accommodate them and you not realizing it had nothing to do with a solution.

NOTE: this solution uses depreciated mysql and not the newer mysqli

This will eliminate duplicates:

while ($row = mysql_fetch_array($results, MYSQL_NUM)) {
   $data[] = serialize(array($row[1],$row[2],$row[3],$row[4],$row[5]));
}
$data = array_unique($data);

To try and keep the first duplicate:

while ($row = mysql_fetch_array($results, MYSQL_NUM)) {
   $data[] = serialize(array($row[1],$row[2],$row[3],$row[4],$row[5]));
}
$data = array_reverse ($data);
$data = array_unique($data);
ksort($data); // puts them back in original order.

OR

while ($row = mysql_fetch_array($results, MYSQL_NUM)) {
  $columns = serialize(array($row[1],$row[2],$row[3],$row[4],$row[5]));
  if (!in_array($columns,$data)){
     $data[] = serialize(array($row[1],$row[2],$row[3],$row[4],$row[5]));
   }
}

Then the new loop is:

foreach($data as $key => $value){
  $row = unserialize($value);  


}

There are other ways to use this to eliminate some dups and exclude others.

$uniqueRows = serialize(array($row[1],$row[2]));  
$data[$uniqueRows] = array($row[3],$row[4],$row[5]);