dpw70180 2015-11-30 14:20
浏览 15
已采纳

如果记录不在SQL中,请创建一个选项

I have a combobox where I need to have on the options names of files in a folder that aren't register in a mySQL database.

The code is:

if ($handle = opendir('../media')) {
    $dbhost = 'HOST';
    $dbuser = 'USER';
    $dbpass = 'PASS';
    $conn = mysql_connect($dbhost, $dbuser, $dbpass);

    while (false !== ($entry = readdir($handle))) {

        if ($entry != "." && $entry != "..") {
            $sql = 'SELECT url FROM musics WHERE url="PREFIX_URL'.$entry.'"';
            mysql_select_db('DATABASE_NAME');
            $retval = mysql_query( $sql, $conn );
            if(! $retval )
   {
        echo "<option value='".$entry."'>";
        echo substr($entry,0,-4);   //This extracts the extension
        echo "</option><br />";
   }
   mysql_close($conn);
   }
   }

    closedir($handle);
}

Database have a column URL on table MUSICS where the content of it on each row is equal to the PREFIX_URL/nameOfTheFile.extension

My problem is that the combobox shows options with names of files that are registered on the database... And this is what I don't want to happen.

Can anybody help me?

  • 写回答

2条回答 默认 最新

  • doudouwd2017 2015-11-30 14:32
    关注

    To make this work, you could do the following.

    use the php function mysql_num_rows ( resource $result )

    So in your code,

       $retval = mysql_query( $sql, $conn );
       $numrows=mysql_num_rows($retval);  //new line inserted
    

    Then in your if statement, change

    if(! $retval )
    

    to

    if ($numrows==0)
    

    What you are achieving is checking how many entries are for the current file. If there are 0 returned by mysql call, then display the select option. Your code before checked to see if mysql worked at all. However - mysql functions in PHP are deprecates, so you should use mysqli.

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

报告相同问题?

悬赏问题

  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统