当我在标记内运行php代码时,我期望'echo'会在标记内回显,但事实并非如此,相反,它会在
之前打印出我在'echoing'的内容。我已经移动了代码,并修复了我所知道的所有内容,但仍然无法正常工作
“ dbh.inc.php”
$servername = "localhost";
$dBUsername = "root";
$dBPassword = "";
$dBName = "hayleyblog";
$conn = mysqli_connect($servername, $dBUsername, $dBPassword, $dBName);
if (!$conn) {
die("Connection failed: ".mysqli_connect_error());
} else {
echo "<p>connected</p>";
}
<!DOCTYPE html>
<?php
require "dbh.inc.php";
?>
<html>
<head>
</head>
<body>
<?php
$conn
?>
</body>
</html>
我希望将“ connected”打印在body标签内部,但将其打印在html标签之前,
答案 0 :(得分:1)
检查连接时,应将值存储在变量中,而不是echo中。
$servername = "localhost";
$dBUsername = "root";
$dBPassword = "";
$dBName = "hayleyblog";
$conn = mysqli_connect($servername, $dBUsername, $dBPassword, $dBName);
if (!$conn) {
die("Connection failed: ".mysqli_connect_error());
} else {
$conn = "<p>connected</p>";
}
然后您可以按如下所示进行回显。
<!DOCTYPE html>
<?php
require "dbh.inc.php";
?>
<html>
<head>
</head>
<body>
<?php
echo $conn;
?>
</body>
</html>
答案 1 :(得分:1)
请理解包含的概念,它可以帮助您在PHP中取得成功。 在其中包含文件的地方
PHP include语句添加您的其他文件代码,在include语句附加到的位置添加一个当前文件。
包含文件意味着您的代码最终将存在
$servername = "localhost";
$dBUsername = "root";
$dBPassword = "";
$dBName = "hayleyblog";
$conn = mysqli_connect($servername, $dBUsername, $dBPassword, $dBName);
$connection_status="";
if (!$conn) {
die("Connection failed: ".mysqli_connect_error());
} else {
$connection_status = "<p>connected</p>";
}
<!DOCTYPE html>
<?php
require "dbh.inc.php";
?>
<html>
<head>
</head>
<body>
<?php
echo $connection_status;
?>
</body>
</html>
尝试此操作,希望您理解是否有任何疑问,请在评论中问我,我可以为您详细解释。您应该深入了解有关PHP include语句的概念。 谢谢