duanmei1694 2016-03-04 20:36
浏览 43
已采纳

如果value = given参数,则停止将条目添加到mysql数据库

I am building a database of latest tracks played on a station and I do not want entries with the station name being added to the database.

I know that I will need to use if else and else if because of what I want to do as I am already checking if entry exists with if and else so am just looking for some pointers on how to stop entries being added if radio name is in the $trackartist field

I know I need to do something along the lines of if $trackartist = station name do nothing else check if entry exists else if do the insert.

Here is the code I am using to fill the rows.

include('config.php');

$str = file_get_contents('xxxxxxxx.xxx');
$json = json_decode($str, true);
$artist = $json['now_playing']['artist'];
$track = $json['now_playing']['track']; 
$cover = $json['cover'];

$check = mysqli_query($mysqli,"select * from recent where      trackartist='$artist' and tracktitle='$track' and coverurl='$cover'");
$checkrows=mysqli_num_rows($check);

if($checkrows>0) {
  echo "track exists";
} else {  

  $sql = "INSERT IGNORE INTO recent (trackartist, tracktitle, coverurl)
  VALUES ('$artist', '$track', '$cover')";
  $result = mysqli_query($mysqli, $sql) or die('Error querying   database.');

  mysqli_close($mysqli);
 };
  • 写回答

2条回答 默认 最新

  • dpuwov1487 2016-03-04 20:51
    关注

    Just nest all of your code in an if statement:

    if($artist != "Station Name"){
    
        $check = mysqli_query($mysqli,"select * from recent where      trackartist='$artist' and tracktitle='$track' and coverurl='$cover'");
        $checkrows=mysqli_num_rows($check);
    
        if($checkrows>0) {
          echo "track exists";
        } else {  
    
          $sql = "INSERT IGNORE INTO recent (trackartist, tracktitle, coverurl)
          VALUES ('$artist', '$track', '$cover')";
          $result = mysqli_query($mysqli, $sql) or die('Error querying   database.');
    
          mysqli_close($mysqli);
         };
    
     }
    

    This will run your code as long as $artist does not equal whatever string you substitute for "Station Name" if it does match this code will do nothing.

    You could also block multiple things if you want like this:

    $starray = array("Station Name 1","Station Name 2","Station Name 3");

    then change if($artist != "Station Name") to if(!in_array($artist,$starray))

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器