dt102282 2014-10-27 14:22
浏览 12
已采纳

PHP MySQLi查询函数解析不正确

I'm trying to fetch data from my MySQL database and display it on a simple webpage.
However when i add the PHP code to the .html file it almost looks like the code isn't parsed as PHP.
The code below connects to the db (which works), I enter a query and then fetch the results.
However given the output it almost looks like the -> in $conn->query($query) is enterpreted as ?>

The page resulting from the code below shows these three lines of text:

 query($query)) {while ($row = $result->fetch_assoc()) {echo ' 

'.$row['Title'].'

';>$result->free();}else"fail";}?>

Which is in accordance to the format i want, but obviously not what intended.
However i get the same resulting page from this code when i remove everything from the second line till $conn->.
Which gives me the impression that the code followed by $conn-> isn't read as PHP code at all.
I think it's also important to note that i don't have this problem in a different part of my page, i.e. creating a database connection works fine and code followed by the query is parsed as i would expect.

Is my assumption correct and how can i counter this? Or have a missed something obvious causing this error?

<div class="col-sm-8 blog-main">
    <?php
    $servername = "localhost";
    $username = "root";
    $password = "marvin";
    $dbname = "blogpostdb";

    $conn = new mysqli($servername, $username, $password, $dbname);
    $query = "SELECT * FROM posts"; 

    if ($result = $conn->query($query)) {

        while ($row = $result->fetch_assoc()) {
        echo '<div class="blog-post"> 
              <h2 class="blog-post-title">'. $row['Title'] .'</h2></div>';
        }
    $result->free();
    }
    else{
    echo "fail";
    }
    ?>
    </div><!-- /.blog-main -->
  • 写回答

1条回答 默认 最新

  • dousi6087 2014-10-27 14:23
    关注

    You need to change the extension from .html to .php.
    Files with the .html extension won't parse PHP, unless the webserver is configured to do so.

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

报告相同问题?

悬赏问题

  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥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失败