【发布时间】:2013-09-12 09:08:48
【问题描述】:
我有三个php文件:engine.php、links.php和test.php
理论上,当我在 test.php 中调用 _insert() 函数时,它应该用 links.php 的输出替换一个字符串,而不是 ob_start() 和 ob_get_clean() 被忽略,而 links.php 的输出只是回显.
engine.php
function db() {
require("config.php");
$conn = @mysql_connect($host, $uname, $pass) or die("DB error: ".mysql_error());
@mysql_select_db($db) or die("DB error: ".mysql_error());
@mysql_query("SET character_set_results = 'utf8', character_set_client = 'utf8', character_set_connection = 'utf8', character_set_database = 'utf8', character_set_server = 'utf8'"); #UTF-8 FIX
}
function _include($x) {
if (preg_match("/<!--Include:(.*)-->/", $x, $matches)){
ob_start();
include($matches[1]);
$output = ob_get_clean();
return preg_replace("/<!--Include:(.*)-->/", $output, $x);
}
}
links.php
<?php
$query = @mysql_query("SELECT title, description, url FROM links ORDER BY id") or die("DB error: ".mysql_error());
while($row = @mysql_fetch_array($query)) {
$url = $row["url"];
$title = $row["title"];
$description = $row["description"];
echo "<a href=\"$url\">$title</a>: $description";
}
@mysql_close($conn) or die(mysql_error());
?>
test.php
<?php
require("engine.php");
db();
echo _include("<div><!--Include:links.php--></div>");
?>
它不是在 div 中输出一系列链接,而是跳过 links.php 的返回完全结束回显,就好像只有一个包含而没有 ob_start() 和 ob_get_clean()。
为什么?
【问题讨论】:
-
你能添加一个结果样本吗?
-
Please, don't use
mysql_*functions in new code。它们不再维护and are officially deprecated。看到red box?改为了解prepared statements,并使用PDO 或MySQLi - this article 将帮助您决定哪个。如果你选择 PDO,here is a good tutorial. -
我必须使用 mysql_* 因为服务器很旧,它的 php 也很旧