嘿,伙计们在一个项目上工作,并且遇到了我的OOP PHP和连接脚本问题。在我的服务器上,我在每个函数中包含'config.php'并且它工作正常,但是当我在LOCALHOST上执行相同的操作时,无法找到变量$ mysqli,PHP版本会不同于服务器到localhost?路径都是100%正确的。 错误如下;
注意:未定义的变量:mysqli in 第20行的C:\ Users \ PC \ Documents \ XAMPP \ htdocs \ php \ myfunctions.php
的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();
}}
}
答案 0 :(得分:0)
不是在每个函数中都使用include 'config.php'; // WHERE TO PUT THIS CANNOT FIND MYSQL
,而是添加$ database参数Eg function mostPopular($database){...
并将$mysqli->
更改为$database->
然后,当您调用函数时,通过mostPopular($database)
不相关的说明:您可能还会发现让函数返回数组而不是回显HTML更容易,因此您的函数只需获取其数据格式并返回值即可。 (这也意味着您可以避免回显完整的HTML。
以下是使用您的mostPopular
函数的示例
功能:
function mostPopular($databaseName){
$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();
$returnData = array();
if($stmt->num_rows() > 0){
$i = 0;
while($row = $stmt->fetch()){
$returnData[$i]['ForumId'] = $ForumId;
$returnData[$i]['ForumTitle'] = $ForumTitle;
$returnData[$i]['ForumPostText'] = substr($ForumPostText, 0,93) . ' ...';
++$i;
}
$stmt->free_result();
}
}
return $returnData;
}
使用:
<div id="mostPopular">
<?php
$mostPopular = mostPopular($mysqli);
if(count($mostPopular) === 0){
?>
<p>No latest article available</p>
<?php
} else {
foreach($mostPopular as $Popular){
?>
<p class="posttitle"><a href="post.php?post=<?php echo $Popular['ForumId'];?>"><?php echo $Popular['ForumTitle'];?></a></p>
<p class="posttext"><?php echo $Popular['ForumPostText'];?></p>
<?php
}
}
?>
</div>
答案 1 :(得分:-2)
问题是config.php脚本不是以
的PHP开始标记启动的<?php
配置文件应该是这样的:
<?php
$mysqli = new mysqli('localhost', 'userone', 'password', 'iitb');
编辑:确保还要检查php_short_tags。可能是config.php文件以短标记<?
开头,并且在本地主机服务器上禁用了short_open_tag。