dongliufa6380
2014-06-03 21:36 浏览 63
已采纳

php显示来自db的数据[关闭]

I'm trying to show data from my first row of db in a html page.. this is: index.html I don't understand where is wrong..

<div class="container">

  <h3>Menu del giorno <?php $currentDate = date("d-m-Y"); echo $currentDate;?></h3><br/>

<?php
$con=mysqli_connect("localhost","root","root","mydb");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$result = mysqli_query($con,"SELECT * FROM menu");


  while($row = mysqli_fetch_array($result)) {

  echo "<p style="text-align: center;"><strong>Cucina:</strong></p><br/><br/>";
  echo "<p>Primi</p><br/>";
  echo "<p>" . $row['piatto1'] . "</p><br/>";
  echo "<p>" . $row['piatto2'] . "</p><br/><br/>";
  echo "<p>Secondo</p><br/>";
  echo "<p>" . $row['secondo'] . "</p><br/>";

  }
  mysqli_close($con);
  ?>

</div>
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答 默认 最新

  • 已采纳
    douda5227 douda5227 2014-06-03 21:43

    First, your extension is wrong. It should, as other users have indicated, be titled index.php instead of index.html.

    Second, you might not be running this on a server. PHP is server side, meaning the code is executed before the user gets it, which is what makes it secure. The downside is that, unlike client-side things like javascript that operate/execute in the browser itself, for PHP you will need an actual server.

    If you aren't already uploading (with an FTP for example) to a server that supports PHP, you will have to make one yourself. An example for mac: MAMP.

    That will also support your SQL DB. Hope this helped!

    EDIT: Oh, I see a new error. When you have a quotation mark within a quotation mark, you need an escape sequence (" \" hello\" "). Otherwise PHP will think the second " is ending the first, instead of actually being in the string. So you should have, for example:

    echo "<p style=\"text-align: center;\"><strong>Cucina:</strong></p><br/><br/>";
    
    点赞 评论 复制链接分享
  • drpph80800 drpph80800 2014-06-03 21:39

    Rename your file from index.html to index.php.

    A HTML file should only contain valid HTML code, where PHP files can contain PHP code statements and are interpreted by your PHP binary (e.g. php.exe on Windows). If you put PHP code in a HTML file this code won't be interpreted. So the PHP code is either way displayed as plain text in your browser, or - in worse case - will invalidate your HTML code. And thus may be block displaying of your page.

    (You can instruct your webserver (e.g. Apache) to treat .html files as PHP files. But I don't think this is the case here.)

    点赞 评论 复制链接分享

相关推荐