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.