mysql和php代码跳过第一个结果

pn9klfpd  于 2021-06-18  发布在  Mysql
关注(0)|答案(2)|浏览(372)

我正在创建一个web应用程序来显示数据库中包含的数据。问题是,当我的应用程序在一个表中显示查询请求的数据时,第一个结果被跳过,我无法取消重新定义原因。此外,控制台中没有显示错误,这就是为什么我无法解决问题的原因。
下面是代码,其中有db连接、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>
rjzwgtxy

rjzwgtxy1#

大音符

每次使用 mysql_ 新代码中的数据库扩展这种情况已经被弃用了很多年,并且在php7.0+中永远消失了。如果你只是在学习php,那么就把精力花在学习 PDO 或者 mysqli 数据库扩展和准备好的语句。从这里开始
删除此行 $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>
          ';

       }
}
?>
py49o6xq

py49o6xq2#

我强烈建议您不要使用mysql\u查询库,因为它已被弃用,而应该使用mysqli库或pdo扩展
下面是我使用mysqli的示例代码。我没有检查它的错误,但你可以看到语法的变化。

$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();

您还可以使用pdo,这也是一些框架开发人员的首选,因为它更安全(对prepared语句的研究),但这里有一个简单的pdo扩展示例

$host = '127.0.0.1';
$db   = 'test';
$user = 'root';
$pass = '';
$charset = 'utf8mb4';

$dsn = "mysql:host=$host;dbname=$db;charset=$charset";
$options = [
    PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
    PDO::ATTR_EMULATE_PREPARES   => false,
];
try {
     $pdo = new PDO($dsn, $user, $pass, $options);
} catch (\PDOException $e) {
     throw new \PDOException($e->getMessage(), (int)$e->getCode());
}

$stmt = $pdo->query("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. "'");
while ($row = $stmt->fetch())
{
    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>";
}

相关问题