【问题标题】:$mysqli variable works on Server but not on localhost$mysqli 变量适用于服务器但不适用于 localhost
【发布时间】: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

标签: php mysql mysqli


【解决方案1】:

不要在每个函数中使用include 'config.php'; // WHERE TO PUT THIS CANNOT FIND MYSQL,而是添加$database 参数例如function mostPopular($database){... 并将$mysqli-&gt; 更改为$database-&gt;

然后当你调用函数时,通过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>

【讨论】:

    【解决方案2】:

    问题是config.php脚本不是用php的开始标签启动的

    <?php
    

    配置文件应该是这样的:

    <?php
    
    $mysqli = new mysqli('localhost', 'userone', 'password', 'iitb');
    

    编辑:确保还检查 php_short_tags。可能是 config.php 文件以短标记 &lt;? 开头,并且在您的 localhost 服务器上禁用了 short_open_tag。

    【讨论】:

    • 他显然只是从文件中取出了那行。他描述说它在他的实时服务器上运行良好,但在 localhost 服务器上却不行。
    • 在这种情况下,它可能是短标签。我已添加到答案中。
    • PHP 标签不是这里的问题。我确定该文件具有 PHP 标记,因为该脚本在他的实时服务器上运行良好,但在他的本地服务器上运行良好。如果标签是问题,脚本将无法在任何 PHP 服务器上运行。
    • 有两个 PHP 开始标签:&lt;?php&lt;?(短标签)。如果生产服务器启用了短标签,并且本地主机禁用了它们,&lt;? 短标签将工作并在生产服务器上被正确解析为 PHP 脚本,但在本地主机上,短标签将被视为纯文本- 因此$mysqli = new MYSQLi 不会被执行。
    猜你喜欢
    • 1970-01-01
    • 2020-09-03
    • 2012-01-01
    • 1970-01-01
    • 2016-04-02
    • 1970-01-01
    • 2019-09-09
    • 2015-07-18
    • 1970-01-01
    相关资源
    最近更新 更多