douyi6168 2015-07-11 05:30
浏览 38
已采纳

我应该在函数内返回DB结果还是循环遍历函数外的结果?

So I was wondering what's the best way/practise of querying the database in a function and then printing the results back to the .php page. I got taught to do the query in the function(), call the function() inside the .php page and then loop through the resultset inside the .php page.

Here is an example of what i'm doing now - Im wondering if this is "Unsafe" or "Not secure" or slower than returning the results in the function? Because at the moment - This is taking a while to load on the page? Could this be because I'm doing using if statements inside the while loop?

     showPlaylist($staticTestUser);


    if($numRecords == 0){
            echo "<div class='no-found'>No playlists found</div>";
        }
        else{
                $htmloutput = "<div class='playlist-wrap'>";

            $i = 1;
            while($arrRows = $stmt->fetch(PDO::FETCH_ASSOC)){
                    $title = $arrRows['song-title']
                    $thumbnail=$arrRows['thumb']

                    $playTitle = $arrRows['playlistName'];
                    $htmloutput .= "<div class='playlist-head'>";
                    //Make sure the title is only shown once - Loop through the rest.
                    if($i == 1){
                        $htmloutput .= $playTitle;
                    }

                    $htmloutput .= "</div>";
                    $htmloutput .= "<div class='playlist-item'>";
                        $htmloutput .= "<div class='play-thumb'>".$thumbnail."</div>";
                        $htmloutput .= "<div class='play-title'>".substr($title, 0,30)."...</div>";
                    $htmloutput .= "</div>";

                    if($i == $numRecords){
                    $playId = $arrRows['pID'];
                    $htmloutput .= "<div class='playlist-footer'>";
                        $htmloutput .= "<div class='play-stats'>";
                        $htmloutput .= "Likes:".$arrRows['likes']." -- Dislikes: ".$arrRows['dislikes'];
                        $htmloutput .= "</div>";
                        $htmloutput .= "<div class='play-vote'>";
                        $htmloutput .= "<button name='playLike' type='submit' value='$playId'>Like</button>";
                        $htmloutput .= "<button name='playDislike' type='submit' value='$playId'>Dislike</button>"; 
                        $htmloutput .= "</div>";
                    $htmloutput .= "</div>";
                }
                $i++;
                    unset($playTitle);
            }
            $htmloutput .= "</div>";

                echo $htmloutput;
        }
  • 写回答

1条回答 默认 最新

  • douhao2153 2015-07-11 05:47
    关注

    There aren't any specific security issues with your code that I can see, but best practice right now is to use the MVC - model, view, controller - design pattern. See here. Or the MVP - model, view, presenter - design pattern. See here. Both are very similar.

    Using one of these patterns has many benefits but the biggest ones in my opinion are code readable, portability, and reusability. It may also speed things up as well.

    The if statements inside your code wouldn't be whats slowing your code down - it would probably be the repeated fetch at the beginning of the while loop.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体