准备好的语句不允许我调用$ mysqli-> stmt_init() [英] Prepared statement not letting me call $mysqli->stmt_init()

查看:52
本文介绍了准备好的语句不允许我调用$ mysqli-> stmt_init()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以前已经做过,但是对mysqli和准备好的语句来说是一个新手(我确定您可以从此问题中看到).

I have done this before but am quite new to mysqli and prepared statements (as I'm sure you can see from this issue).

我要去哪里错了?

这是我的连接功能(属于"Connect"类)

here is my connection function (part of the 'Connect' class)

public function site_db()
{
    // Connect to MySQL 
    $link = mysqli_connect(SITE_HOST, SITE_ID, SITE_PW, SITE_DB); 

    // Check for Errors 
    if(mysqli_connect_errno()) 
    { 
        //echo mysqli_connect_error(); //shouldnt show client specific error information. 
        die('Error connecting to mysql database please report.'); 
    }
}

这里是引起错误的函数:

Heres the function which is causing the error:

public function exists ($what, $who)
{

    $query = "SELECT * FROM users WHERE ? = ?";

    // Get instance of statement 
    $stmt = $mysqli->stmt_init();

    // Prepare query 
    if($stmt->prepare($query)) 
    { 
        // Bind Parameters 
        $stmt->bind_param("ss", $what, $who); 

        // Execute statement 
        $stmt->execute(); 

        // Bind result variables 
        $stmt->bind_result($result); 

        // Fetch Value 
        $stmt->fetch(); 

        // catch num_rows result as variable
        $username_result = $result->num_rows;

        // Close Statement 
        $stmt->close();
    }

    if ($username_result != 0)
    {
        return true;
        echo 'true';
    }
    else
    {
        return false;
        echo 'false';
    }
}

我得到的错误:

PHP致命错误:在X行的somefile.php中的非对象上调用成员函数stmt_init()

PHP Fatal error: Call to a member function stmt_init() on a non-object in somefile.php on line X

它指的是这一行:

$stmt = $mysqli->stmt_init();

我在这里犯了一个愚蠢的错误吗?我怎么不能打电话呢?

am I making a stupid error here? Howcome I can't call that?

编辑//

注意:我没有说清楚,但是这两个函数在不同的类中.

NOTE: I didn't make this very clear, but these two functions are within different classes.

推荐答案

public function site_db()
{
    // Connect to MySQL 
    $mysqli = mysqli_connect(SITE_HOST, SITE_ID, SITE_PW, SITE_DB); 

    // Check for Errors 
    if(mysqli_connect_errno()) 
    { 
        //echo mysqli_connect_error(); //shouldnt show client specific error information. 
        die('Error connecting to mysql database please report.'); 
    }

     return $mysqli;
}


public function exists (Mysqli $mysqli, $what, $who)
{

    $query = "SELECT * FROM users WHERE ? = ?";

    // Get instance of statement 
    $stmt = $mysqli->stmt_init();

    // Prepare query 
    if($stmt->prepare($query)) 
    { 
        // Bind Parameters 
        $stmt->bind_param("ss", $what, $who); 

        // Execute statement 
        $stmt->execute(); 

        // Bind result variables 
        $stmt->bind_result($result); 

        // Fetch Value 
        $stmt->fetch(); 

        // catch num_rows result as variable
        $username_result = $result->num_rows;

        // Close Statement 
        $stmt->close();
    }

    if ($username_result != 0)
    {
        return true;
        echo 'true';
    }
    else
    {
        return false;
        echo 'false';
    }
}

使用方法:

实例化具有site_db()方法的第一类

Instantiate first class that have site_db() method

$db = new Class();

然后实例化具有exist()方法的第二个类

Then instantiate second class that have exist() method

$query = new Class();

然后简单地

$query->exist($db->site_db(), $what, $who );

这篇关于准备好的语句不允许我调用$ mysqli-> stmt_init()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆