从列表中选择数据库,然后在PHP中查询它

时间:2016-12-13 09:59:56

标签: php mysql

我想知道我的代码中缺少的内容。

我有这个HTML代码:

<html>
  <head>
    <title>Human gene catalog</title>
  </head>
<body>
  <h1>Reference sequence and Gene Ontology catalog</h1>
  <p>
    <b>1.Search for a gene:</b>
    <form action=prueba.php method=post>
      <select name=organism> 
        <option value=1>Human</option> 
        <option value=2>Mouse</option> 
        <option value=3>Zebrafish</option> 
        <option value=4>Fruit fly</option> 
      </select>
      <label>Please select a gene:</label>
      <br/>
      <input type=text name=gene>
      <br/><br/>
      <input type=submit name=submit value=Submit>
    </form>
  </p>
</html>

这个PHP代码:

 <?php
   $gene = $_POST["gene"];
   $specie = $_POST["organism"];

   $enlace = mysqli_connect("localhost","root","******","refGene");
   if (mysqli_connect_errno()) {
       echo "Failed to connect to MySQL: " . mysqli_connect_error();
   }

   if ($specie == 1) {
       mysqli_select_db($enlace,"refGene_human");
   } elseif ($specie == 2) {
       mysqli_select_db($enlace,"refGene_mouse");
   } elseif ($specie == 3) {
       mysqli_select_db($enlace,"refGene_zebrafish");
   } elseif ($specie == 4) {
       mysqly_select_db($enlace,"refGene_fruitfly");
   } else {
       echo "The gene is not in database";
   }

   $result = mysqli_query($enlace,"select * from (here I dont know what to put,if with one specie, here I put the name of the table) where name2 like '%$gene%'");

   echo "<h1>Gene Reference Results</h1>";

   echo "<table cellspacing=3 cellpadding=4 border=1 bgcolor=#dddddd>";
   echo "<tr align='center'><th>Transcript</th><th>Gene</th <th>Chromosome</th><th>Strand</th><th>Gene_Start</th><th>Gene_End</th><th>CDS_Start</th><th>CDS_End</th><th>ExonCount</th>";

   while ($extraido = mysqli_fetch_array($result)){
       echo "<tr>";
       echo "<td>".$extraido['name']."<br/>";
       echo "<td>".$extraido['name2']."<br/>";
       echo "<td align='center'>".$extraido['chrom']."<br/>";
       echo "<td align='center'>".$extraido['strand']."<br/>";
       echo "<td align='right'>".$extraido['txStart']."<br/>";
       echo "<td align='right'>".$extraido['txEnd']."<br/>";
       echo "<td align='right'>".$extraido['cdsStart']."<br/>";
       echo "<td align='right'>".$extraido['cdsEnd']."<br/>";
       echo "<td align='right'>".$extraido['exonCount']."<br/>";
   }
   echo "</table>";

   mysqli_free_result($result);
   mysqli_close($enlace);

基本上我想要实现的是,在HTML文档中选择一个物种和一个基因名称,它会转到PHP,在那里,它取决于它选择的物种(1,2,3,4)的值相应的数据库,然后查询它以查找一些信息。

我有正确的代码使其工作,但只有一个物种,没有HTML中的选择列表和PHP中的if语句,但我想让它在多个工作。

我想我应该在PHP中的if语句之前声明一些变量,然后查询这个应该告诉查询函数选择哪个表的变量。

但我不知道什么是正确的语法。

1 个答案:

答案 0 :(得分:0)

if ($specie == 1) {
    mysqli_select_db($enlace,"refGene_human");
    $database = 'refGene_human';
} 
elseif ($specie == 2) {
    mysqli_select_db($enlace,"refGene_mouse");
    $database = 'refGene_mouse';
} 
elseif ($specie == 3) {
    mysqli_select_db($enlace,"refGene_zebrafish");
    $database = 'refGene_zebrafish';
} 
elseif ($specie == 4) {
    mysqly_select_db($enlace,"refGene_fruitfly");
    $database = 'refGene_fruitfly'; 
} 
else { 
    echo "The gene is not in database";
}

$result = mysqli_query($enlace,"select * from '$database' where name2 like '%$gene%'");