2

I am trying to create a php based search function, with following code:

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

?>

The problem, which is rather unique, is that this code is displaying those queries which have duplicate entries(and even for those, it is displaying result for the last row, not all rows with duplicate OMIM_ID). And for unique OMIM_ID, there are no results, even though there is no explicit error.

Table Scheme: int OMIM_ID varchar2 location(chromosome) varchar2 phenotype int MIMnumber varchar2 Gene int geneMIMNumber int geneid varchar2 Protein

Any clues on what I am doing wrong ?

--Update--

Even after updating my code to:

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

I am getting same error.

Update: A more clear view on problem.

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

Now, given 2141 as query, the code will display 2nd entry in result, not the first one, which should have been included too.

If 2142 is the query, there is no output, ideally, the possible output should have been the row described by OMIM_ID 2142.

Siddharth
  • 373
  • 2
  • 17
  • 6
    Please, [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and consider using [PDO](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard May 04 '15 at 18:12
  • @JayBlanchard, will look through it, but I was just wondering what is wrong with this code. – Siddharth May 04 '15 at 18:15
  • 2
    @Siddharth: You mean apart from the fact that you're using a deprecated extension, and you're relying on mysql's implicit fallback to the last active connection, _and_ you're echoing markup (don't do that, seriously), **and** you seem to be forgetting that arrays are zero indexed (`$rows[0]` is the first element), ***and*** you're vulnerable to injection? – Elias Van Ootegem May 04 '15 at 18:22
  • @EliasVanOotegem, I understand, I understand!! Well I am not much into web and scripting languages and specifically not into PHP much, but yeah, I will try to use PHP PDO. I was not planning to get a *thrashing* in that manner xD. Oh, and as for indexing, I forgot to mention that there is another element which was not necessary for result, so I started with 1. – Siddharth May 04 '15 at 18:27
  • @EliasVanOotegem, changed my code as suggested. – Siddharth May 04 '15 at 18:53
  • 1
    I'm gonna upvote for the sole fact that you took @JayBlanchard's comment seriously and overhauled your whole code. Good job! – Mike May 04 '15 at 20:38

1 Answers1

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]);
Misunderstood
  • 5,534
  • 1
  • 18
  • 25
  • Ah, my sympathies. I mean, I am searching with OMIM_ID, and lets say OMIM_ID contains two entries with same value, 12345. Now, my code will display me the second row containing 12345, not the first one. Another scenario is that if OMID_ID xyz is unique and appearing in a single row, there is not output at all. – Siddharth May 04 '15 at 19:56
  • I updated my answer to reverse the arrays order. Not sure it will help. – Misunderstood May 04 '15 at 20:00
  • Thanks, I will try the given suggestion, although I don't think this will help much. Maybe it has to do something with the way results are being fetched. *Off-Topic start* *As for comments above in original question, I don't see the rationale behind implying a newer standard **to be THE solution** over the asked query, rather than actually helping with query. I don't think it will be fair to suggest C++ 14 standard for a question related to simple mathematical operation.* *End of off-topic stuff* – Siddharth May 04 '15 at 20:15
  • Like I said I do not fully understand your problem. What went wrong with the array_reverse? – Misunderstood May 04 '15 at 20:18
  • Do you have a link to your work in process? It might help if I saw the actual results and then you could point out the issue in the results. – Misunderstood May 04 '15 at 20:22
  • I added a couple more cases that would help keep the first dups. – Misunderstood May 04 '15 at 20:30
  • I have tried to explain my situation more clearly in main question. Also, I will try to move the whole setup to live system if possible. – Siddharth May 04 '15 at 20:31
  • So out of those 3 rows you only want rows 1 and 2 but with my original solution you are getting 2 and 3? – Misunderstood May 04 '15 at 20:36
  • Your solution is not giving me the third row when I am entering its respective OMIM_ID. I think I will check the schema and rebuild the whole database. – Siddharth May 04 '15 at 20:43
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/76933/discussion-between-siddharth-and-misunderstood). – Siddharth May 04 '15 at 21:03
  • I will be marking this as answer since the method worked flawlessly on other table. Still, I will try to answer my own question later by finding the actual bug in code. – Siddharth May 05 '15 at 05:18