doulingqiu4349 2013-11-24 20:12
浏览 5
已采纳

PHP功能问题[重复]

This question already has an answer here:

I have problem with functions. I write them in config/funkcije.php. In folder config/ I have db.php which connects to database, etc... When I open in browser config/funkcije.php nothing shows on that page but it should echo out some results from DB.

This is my function:

include 'db.php';
function prikaz_posebne_ponude()
{

    $sql  = "SELECT * FROM posebna_ponuda ORDER BY id DESC LIMIT 1";
    $sql1 = $mysqli->query("$sql");
    while ($row = $sql1->fetch_assoc()) {
        $glavni_naziv = $row[$lang];
        if (empty($glavni_naziv)) {
            echo '';
        } else {

            echo "<div class='row-banner'>";
            echo "<h3><span>" . $langArray['rezervacija_smjestaja'] . "</span></h3>";
            echo "<p>" . $glavni_naziv . "</p>";
            echo "</div>";

        }
    }
}

But when I remove function prikaz_posebne_ponude(){ and } on the last line everything works fine. Why is this happening?

</div>
  • 写回答

4条回答 默认 最新

  • dqroc48068 2013-11-24 20:16
    关注

    You define a function, but you never call it. Functions are reusable pieces of code, but to execute the contained statements, you have to call the function like this:

    prikaz_posebne_ponude();
    

    You also need to tell PHP that some variables are global (inside your function):

    global $mysqli;
    global $langArray;
    global $lang;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?