doutongwei4380 2017-01-02 11:51
浏览 67
已采纳

如何检查数据库中是否存在值,然后返回数据行

this is for some coursework for my A-level comp sci course. I am currently coding in a language quite unfamiliar and i need some help with an aspect i have been struggling with for a while now. Below shows what I have done so far. What i want to do is output all values in the table that correspond have a certain sport ID this is what I have so far but it doesn't return any values although the table is populated.

<?php
$link = mysqli_connect("localhost", "root", "pizza","fixtures");

if ($_POST['SPORT'] == "Football") {
    $sp = '1';
}
if ($_POST['SPORT'] == "Tennis") {
    $sp = '2';
}
if ($_POST['SPORT'] == "Swimming") {
    $sp = '3';
}

$result = mysql_query("SELECT * FROM fixtureDetails WHERE fixtureDetails.sportID = '$sp'");
if(mysqli_num_rows($result) > 0) {
    echo "yes";
}
mysqli_close($link);
?>
  • 写回答

1条回答 默认 最新

  • du521521521 2017-01-02 11:53
    关注

    Your code has a few issues:

    1. Don't mix more than one library. You are using mysql_ and mysqli_ together.
    2. Don't use mysql_* functions. They are deprecated.
    3. No need of mysqli_close() function.
    4. You don't need to repeat the table.
    5. You are not printing anything from the query's result.

    The problem with your code is, you need to change this line:

    $result = mysqli_query($link, "SELECT * FROM `fixtureDetails` WHERE `sportID`='$sp'");
    

    To populate as a table, use the resultset and loop.

    if (mysqli_num_rows($result)) {
      while (false != ($data = mysqli_fetch_assoc($result))) {
        // Do whatever with your data.
        var_dump($data);
      }
    } else {
      echo "No records.";
    }
    

    Final Code

    <?php
      $link = mysqli_connect("localhost", "root", "pizza","fixtures");
    
      if ($_POST['SPORT'] == "Football") {
        $sp = '1';
      }
      if ($_POST['SPORT'] == "Tennis") {
        $sp = '2';
      }
      if ($_POST['SPORT'] == "Swimming") {
        $sp = '3';
      }
    
      // Execute the query and save the resultset.
      $result = mysqli_query($link, "SELECT * FROM `fixtureDetails` WHERE `sportID`='$sp'");
      // Check if there are any rows returned.
      if (mysqli_num_rows($result)) {
        // If there are rows returned, save every row to $data.
        while (false != ($data = mysqli_fetch_assoc($result))) {
          // Do whatever with your data.
          var_dump($data);
        }
      } else {
        // If there are no records, display a message.
        echo "No records.";
      }
    ?>
    

    If you want a function to send the response of the count, you can have something like this:

    <?php
      function getCount() {
        $link = mysqli_connect("localhost", "root", "pizza","fixtures");
    
        if ($_POST['SPORT'] == "Football") {
          $sp = '1';
        }
        if ($_POST['SPORT'] == "Tennis") {
          $sp = '2';
        }
        if ($_POST['SPORT'] == "Swimming") {
          $sp = '3';
        }
    
        // Execute the query and save the resultset.
        $result = mysqli_query($link, "SELECT * FROM `fixtureDetails` WHERE `sportID`='$sp'");
        // Check if there are any rows returned.
        return mysqli_num_rows($result);
      }
    ?>
    

    If you want just a true or false, you can do:

    <?php
      function getCount() {
        $link = mysqli_connect("localhost", "root", "pizza","fixtures");
    
        if ($_POST['SPORT'] == "Football") {
          $sp = '1';
        }
        if ($_POST['SPORT'] == "Tennis") {
          $sp = '2';
        }
        if ($_POST['SPORT'] == "Swimming") {
          $sp = '3';
        }
    
        // Execute the query and save the resultset.
        $result = mysqli_query($link, "SELECT * FROM `fixtureDetails` WHERE `sportID`='$sp'");
        // Check if there are any rows returned.
        return (mysqli_num_rows($result) > 0);
      }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ansys fluent计算闪退
  • ¥15 有关wireshark抓包的问题
  • ¥15 需要写计算过程,不要写代码,求解答,数据都在图上
  • ¥15 向数据表用newid方式插入GUID问题
  • ¥15 multisim电路设计
  • ¥20 用keil,写代码解决两个问题,用库函数
  • ¥50 ID中开关量采样信号通道、以及程序流程的设计
  • ¥15 U-Mamba/nnunetv2固定随机数种子
  • ¥15 vba使用jmail发送邮件正文里面怎么加图片
  • ¥15 vb6.0如何向数据库中添加自动生成的字段数据。