MySQL和PHP代码跳过第一个结果

时间:2018-11-14 09:44:44

标签: php mysql

我正在创建一个Web应用程序来播放数据库中包含的数据。我用php和mysql语言编写了代码。问题是,当我的应用程序在表中显示查询所请求的数据时,第一个结果将被跳过,我无法理解原因。 此外,控制台中未显示任何错误,这就是为什么我无法解决问题的原因。

这里是代码,其中包含数据库连接,mysql查询和表中的输出组织。

<html>
<body>

<div id="holder">
<h1><?php echo "<font face=verdana size=30 color=#036>SEQUENZA DI START</font>";?></h1>
</div>
<br>

<div class="container">
<?php
$conn= mysql_connect("db", "db", "ps");
if (!$conn)
{
     die("Connessione non riuscita <br>" . mysql_error());
//}else{
    //echo "Connessione al database stabilita con successo<br><br>";
}

mysql_select_db("var", $conn);

if(isset($_POST["vai"])){
   $start=$_POST["start"];
}
echo "Verranno visualizzate le varianti per la seguente sequenza di start: '<b>$start</b>'.";?>
<br><br>
      <div class="row">
            <div class="col-mid-8 col-mid-offset-2">
                 <table id="mytable" class="table table-striped table-bordered table-hover">
                      <thead>
                        <tr>
                          <th><b>Codice DNA</b></th>
                          <th><b>Chr</b></th>
                          <th><b>Start</b></th>
                          <th><b>End</b></th>
                          <th><b>Alt</b></th>
                          <th><b>Ref</b></th>
                          <th><b>Zygosity</b></th>
                          <th><b>Coverage</b></th>
                          <th><b>InsertPos</b></th>
                        </tr>
                      </thead>
                      <tbody>   
                         <?php
                             $sql="SELECT V.dnaCode, V.Chr, V.Start, V.End, V.Alt, V.Ref, V.zygosity, V.coverage, V.InsertPos FROM variante as V WHERE V.Start='" .$start. "'";
                             $result_start = mysql_query($sql, $conn) or die(mysql_error());
                             $record_start=mysql_fetch_array($result_start);
                             if($record_start==false)
                            {
                                  echo "<br>La ricerca non ha prodotto alcun risultato con la sequenza di start inserita!<br>";
                                  echo"<b>Effettuare una nuova ricerca.</b><br><br>";
                            }else
                            {

                         while ($record_start = mysql_fetch_array($result_start)) {
                             echo '
                                <tr>
                                  <td>'.$record_start["dnaCode"].'</td>
                                  <td>'.$record_start["Chr"].'</td>
                                  <td>'.$record_start["Start"].'</td>
                                  <td>'.$record_start["End"].'</td>
                                  <td>'.$record_start["Alt"].'</td>
                                  <td>'.$record_start["Ref"].'</td>
                                  <td>'.$record_start["zygosity"].'</td>
                                  <td>'.$record_start["coverage"].'</td>
                                  <td>'.$record_start["InsertPos"].'</td>
                                </tr>
                                ';

                             }
                         };
                       ?>

                    </tbody>
               </table>                       
            </div>
       </div>
  </div>


         <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous">         </script>
         <script type="text/javascript" src="js/ddtf.js"></script>
         <script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.js"></script>

         <script type="text/javascript" >
         $('#mytable').ddTableFilter();
         </script> 
</body>
</html>

2 个答案:

答案 0 :(得分:1)

  

大笔记

     

每次使用the mysql_    新代码中的数据库扩展     this happens    它已弃用,并且已经存在多年了,并且在PHP7.0 +中已经永远消失了。   如果您只是在学习PHP,请花精力学习PDOmysqli数据库扩展和准备好的语句。   Start here

删除该行$record_avsnp150 = mysql_fetch_array($result_avsnp150);,因为它正在从结果集中获取第一行,但您对此无所事事。

$sql_avsnp150= "SELECT A.avsnp150, V.dnaCode, V.Chr, A.Start, A.End,
                       A.Alt, A.Ref, V.zygosity, A.gene, A.Func 
                FROM annotazioni as A 
                    JOIN variante as V ON A.Start = V.Start 
                        AND A.Alt=V.Alt 
                        AND A.Ref=V.Ref 
                WHERE A.avsnp150='".$avsnp150."'";
$result_avsnp150 = mysql_query($sql_avsnp150, $conn) or die(mysql_error());;

// remove this line
//$record_avsnp150 = mysql_fetch_array($result_avsnp150);

// and change this one too
//if($record_avsnp150==false)
if($result_avsnp150 == false ) {
    echo mysqli_error();
    exit;  // serious error so no point continuing
}

if ( mysql_num_rows($result_avsnp150) == 0)    {
    echo "<br>La ricerca non ha prodotto alcun risultato con l'avsnp150 inserito!<br>";
    echo"<font color=#039><b>Effettuare una nuova ricerca.</b></font><br><br>";
}else{
    while ($record_avsnp150 = mysql_fetch_array($result_avsnp150)) {       
       echo ' 
          <tr>
            <td>'.$record_avsnp150["avsnp150"].'</td>
            <td>'.$record_avsnp150["dnaCode"].'</td>
            <td>'.$record_avsnp150["Chr"].'</td>
            <td>'.$record_avsnp150["Start"].'</td>
            <td>'.$record_avsnp150["End"].'</td>
            <td>'.$record_avsnp150["Alt"].'</td>
            <td>'.$record_avsnp150["Ref"].'</td>
            <td>'.$record_avsnp150["zygosity"].'</td>
            <td>'.$record_avsnp150["gene"].'</td>
            <td>'.$record_avsnp150["Func"].'</td>
          </tr>
          ';

       }
}
?>

答案 1 :(得分:0)

我强烈建议您不要使用 mysql_query 库,因为不建议使用它,而应使用MySQLi库或PDO扩展

这是我使用mysqli的示例代码。我没有检查错误,但是您可以看到语法发生了变化。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="right_btn">
left
</button>
<button id="left_btn">
right
</button>


<div id="container">



<div id="image_one_div">

</div>

<div id="image_two_div">

</div>
<div id="image_three_div">


</div>

</div>

您还可以使用PDO,它是某些框架开发人员所偏爱的,因为它更安全(对预处理语句进行研究),但这是该PDO扩展的简单示例

$conn = mysqli_connect("host", "user", "pass", "db");

if (!$conn)
{
     die("Connessione non riuscita <br>" . mysql_error());
}

$sql="SELECT V.dnaCode, V.Chr, V.Start, V.End, V.Alt, V.Ref, V.zygosity, V.coverage, V.InsertPos FROM variante as V WHERE V.Start='" .$start. "'";

$result = $db->query($sql);
if($result){
     // Cycle through results
    while ($row = $result->fetch_assoc()){
       echo "
       <tr>
         <td>'.$row["dnaCode"].'</td>
         <td>'.$row["Chr"].'</td>
         <td>'.$row["Start"].'</td>
         <td>'.$row["End"].'</td>
         <td>'.$row["Alt"].'</td>
         <td>'.$row["Ref"].'</td>
         <td>'.$row["zygosity"].'</td>
         <td>'.$row["coverage"].'</td>
         <td>'.$row["InsertPos"].'</td>
       </tr>";
    }
    // Free result set
    $result->close();
}


$db->close();