【发布时间】:2014-04-19 19:31:10
【问题描述】:
在我的服务器上,我在每个函数中都包含“config.php”并且它运行良好,但是当我在我的 LOCALHOST 上执行相同操作时,找不到变量 $mysqli,PHP 版本是否会因服务器与 localhost 不同而有所不同?路径都是 100% 正确的。 错误如下;
注意:未定义变量:mysqli in C:\Users\PC\Documents\XAMPP\htdocs\php\myfunctions.php 在第 20 行
config.php
$mysqli = new mysqli('localhost', 'userone', 'password', 'iitb');
当我使用服务器时,连接明显改变
myfunctions.php
<?php
class News
{
function getLatest()
{
include 'config.php'; // WHERE TO PUT THIS CANNOT FIND MYSQL
$time = date('Y-m-d G:i:s', strtotime("-1 week"));
$stmt = $mysqli->prepare("SELECT ForumId, ForumTitle, ForumPostText FROM `forum` WHERE `PostDate` > ? ORDER BY PostDate desc LIMIT 5 ");
$stmt->bind_param('s', $time);
$stmt->execute();
$stmt->bind_result($ForumId, $ForumTitle, $ForumPostText);
$stmt->store_result();
if ($stmt->num_rows() == 0) {
echo "<p>No latest article available</p>";
} else {
while ($row = $stmt->fetch()) {
echo '<p class="posttitle"><a href="post.php?post=' . $ForumId . '">' . $ForumTitle . '</a> </p>';
echo '<p class="posttext">' . substr($ForumPostText, 0, 93) . ' ...</p>';
}
$stmt->free_result();
}
}
function mostPopular()
{
include 'config.php'; // WHERE TO PUT THIS CANNOT FIND MYSQL
$stmt = $mysqli->prepare("SELECT ForumId, ForumTitle, ForumPostText FROM forum ORDER BY Views DESC LIMIT 5");
$stmt->execute();
$stmt->bind_result($ForumId, $ForumTitle, $ForumPostText);
$stmt->store_result();
if ($stmt->num_rows() == 0) {
echo "<p>No latest article available</p>";
} else {
while ($row = $stmt->fetch()) {
echo '<p class="posttitle"><a href="post.php?post=' . $ForumId . '">' . $ForumTitle . '</a> </p>';
echo '<p class="posttext">' . substr($ForumPostText, 0, 93) . ' ...</p>';
}
$stmt->free_result();
}
}
}
【问题讨论】:
-
在你的 php.ini 中启用 mysqli 扩展...
-
正如 Svetlio 所说,您可能禁用了 MySQLi 扩展。检查 phpinfo();或在 php.ini 中。
-
如果禁用 MYSQLi,它不会产生“PHP 致命错误:找不到类 'MYSQLi'”吗?
-
@MichaelYoo 如果 error_reporting 已关闭,则不会。
-
@Svenskunganka 如果他关闭了 error_reporting,为什么他会收到
Notice: Undefined variable?