PHP 包含两个文件

问题描述 投票:0回答:1

PHP 新手,并且对包含两个包含语句的页面有问题。

我有一个文件包含我的数据库连接代码,另一个文件包含列出数据库信息的代码。数据库连接文件中的代码运行良好,但第二个文件中的代码运行不正常。

为了测试,我已经从第二个文件中取出了所有数据库内容,只留下了一堆 echo 语句,但该代码仍然无法运行。

我将“Testing1”和“Testing2”放在主页中以进行故障排除。加载页面时会显示“已成功建立与 MySQL 服务器的连接”和“测试 1”,但没有其他内容。

主页

  <?php include 'db.php';?>
<html>
<Head>
    <Title>
        Tracked Items
    </title>
</head>
<body>
  Testing1<br>
<?php include 'ListAll.php';?>
  Testing2<br>
</body>
<?php $conn->close(); ?>
</html>

db.php

<?php
  $host_name = 'xxxx';
  $database = 'xxxx';
  $user_name = 'xxxx';
  $password = 'xxxx';

  $link = new mysqli($host_name, $user_name, $password, $database);

  if ($link->connect_error) {
    die('<p>Failed to connect to MySQL: '. $link->connect_error .'</p>');
  } else {
     echo '<p>Connection to MySQL server successfully established.</p>';
  }
?>

ListAll.php


<?php
echo "<b>Current Items</b><br>";
echo "<Table border=""1"">";
echo "  <tr>";
echo "      <td width=""300""><b>Description</b></td>";
echo "      <td width=""80""><b>Date Opened</b></td>";
echo "      <td width=""80""><b>Last Action Date</b></td>";
echo "      <td width=""750""><b>Last Action</b><td>";
echo "</tr>";
?>
php include
1个回答
0
投票

你的问题是双引号。因此,要么对双引号字符串内的属性值使用单引号,如下所示

echo "<b>Current Items</b><br>";
echo "<Table border='1'>";
echo "  <tr>";
echo "      <td width='300'><b>Description</b></td>";
echo "      <td width='80'><b>Date Opened</b></td>";
echo "      <td width='80'><b>Last Action Date</b></td>";
echo "      <td width='750'><b>Last Action</b><td>";
echo "</tr>";

OR 单引号 echo 并使用双引号作为属性,当然只有一组双引号

echo '<b>Current Items</b><br>';
echo '<Table border="1">';
echo '  <tr>';
echo '      <td width="300"><b>Description</b></td>';
echo '      <td width="80"><b>Date Opened</b></td>';
echo '      <td width="80"><b>Last Action Date</b></td>';
echo '      <td width="750"><b>Last Action</b><td>';
echo '</tr>';
© www.soinside.com 2019 - 2024. All rights reserved.