【问题标题】:Changing from MySQL to MySQLi. Select Inside Function in a Class does not work [duplicate]从 MySQL 更改为 MySQLi。在类中选择内部函数不起作用[重复]
【发布时间】:2015-10-12 14:40:02
【问题描述】:

这是一个简单的问题,但由于某种原因我没有看到它。 我正在将一段代码从 MySQL 更改为 MySQLi。问题是当 我用的是新的 MySQLi 代码,select 不起作用。

我正在展示三段代码。 1- 包含代码的 Index.php 文件 2. Connect.php 文件显示我如何连接到数据库 3. Redirect.php 类代码使用了一个失败的函数。

在底部的 Redirect.php 中,我包含了有效的旧代码 以及不起作用的新代码。我所看到的只是新的 代码正在使用 $con 与数据库通信,我尝试了 global $con 和 它也失败了。拜托我需要你的帮忙。你能告诉我我做错了什么吗? 谢谢。

<?php 
/*******************************/
/*  HERE IS THE INDEX.PHP FILE */
/*******************************/
session_start();
    include("connection.php");
    include("redirect.php");
    $log=new redirect();
    $log->check_logged();               
    $theme=$log->get_theme();
    echo 'This is the Website Theme: ' . $theme . '<br>';
    exit;
?>

<?php 
/**************************************************/
/* HERE IS THE CONNECTION.PHP CODE BEING INCLUDED */
/**************************************************/
$con = mysqli_init( );
if (!$con)
    {
        die("ERROR: MySQLi Initialed failed");
    }

if (!mysqli_real_connect($con, SERVERNAME,USERNAME,PASSWORD,DATABASENAME, NULL, NULL, MYSQL_CLIENT_INTERACTIVE))
    {
        die("MySQLi Real Connect Error: " . mysqli_connect_error());
    }
?>

<?php 
/**************************************************/
/* HERE IS THE REDIRECT.PHP CODE BEING INCLUDED */
/**************************************************/
class redirect
{
    function __construct()
    {   }

    function check_logged()
    {
        if(isset($_SESSION['member_id']) && $_SESSION['member_id']!="")
        {
            if(isset($_GET['loginad']) && $_GET['loginad']==1)
                header('location:'.SITEADDRESS_MEMBER.'memberoverview.php?loginad=1');
            if(isset($_GET['surfad']) && $_GET['surfad']==1)
                header('location:'.SITEADDRESS_MEMBER.'memberoverview.php?surfad=1');   
            else
                header('location:'.SITEADDRESS_MEMBER.'memberoverview.php');
        }
    }

    function get_theme()
    {
        /************************************************/
        /* HERE IS THE OLD REDIRECT.PHP CODE THAT WORKS */
        /************************************************/
        $result = mysql_query("select * from theme where status='yes'");
        if(mysql_num_rows($result)>0)
        {
            $data=mysql_fetch_array($result);
            return $data['theme_name'];
        }


        /********************************************************************/
        /* HERE IS THE NEW REDIRECT.PHP CODE BEING TESTED THAT DOESN'T WORK */
        /********************************************************************/
        $result = mysqli_query($con, "select * from theme where status='yes'")
        if(mysqli_num_rows($result)>0)
        {
            $data=mysqli_fetch_array($result, MYSQLI_ASSOC);
            return $data['theme_name'];
        }
    }
}
?>

【问题讨论】:

  • redirect::get_theme 应该如何访问 var $con ?不在他的范围内。

标签: php mysql mysqli


【解决方案1】:

还有其他方法,但主要问题是 $con 在课堂上不可用。你可以传入构造函数,设置它并在类中使用$this-&gt;con

include("connection.php"); // $con is defined here
$log = new redirect($con); // pass it to __construct()

class redirect
{
    public $con;

    function __construct($con) // accept it here
    {
        $this->con = $con;     // set it here
    }

    function get_theme()
    {
        // use it here
        $result = mysqli_query($this->con, "select * from theme where status='yes'");
    }
}

【讨论】:

  • 这个解决方案的问题是 TPdeveloper 需要重写 rewrite 使用 mysqli_query 的每个构造函数和对象实例
  • 你是给我的答案打-1的人吗?
  • 不,但我会做到-2。
  • 你能告诉我为什么吗?
  • 只是因为你在问:-)
【解决方案2】:

您可以使用单例来实现只有一个连接,并且在需要时可以轻松访问:

class connection{

    private $con = null;

    public static function getCon()
    {
        if(null === $this->con)
        {

            $con = mysqli_init( );
            if (!$con)
            {
                die("ERROR: MySQLi Initialed failed");
            }

            if (!mysqli_real_connect($con, SERVERNAME,USERNAME,PASSWORD,DATABASENAME, NULL, NULL, MYSQL_CLIENT_INTERACTIVE))
            {
                die("MySQLi Real Connect Error: " . mysqli_connect_error());
            }

            $this->con = $con;
        }

        return $this->con;
    }
}

然后在需要的时候调用它

function get_theme()
{
    /********************************************************************/
    /* HERE IS THE NEW REDIRECT.PHP CODE BEING TESTED THAT DOESN'T WORK */
    /********************************************************************/
    $result = mysqli_query(connection::getCon(), "select * from theme where status='yes'")
    if(mysqli_num_rows($result)>0)
    {
        $data=mysqli_fetch_array($result, MYSQLI_ASSOC);
        return $data['theme_name'];
    }
}

有关信息,您可以在 POO 模式下使用 mysqli。 我会推荐它,因为您可以轻松地直接返回 mysqli 对象,而不仅仅是 $con

【讨论】:

    猜你喜欢
    • 2012-06-05
    • 2013-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-05
    • 2015-07-19
    • 1970-01-01
    相关资源
    最近更新 更多