Mysql
 sql >> Base de données >  >> RDS >> Mysql

Instruction préparée ne me laissant pas appeler $mysqli->stmt_init()

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';
    }
}

Comment utiliser :

Instanciez la première classe qui a la méthode site_db()

$db = new Class();

Ensuite, instanciez la deuxième classe qui a la méthode exist()

$query = new Class();

Alors simplement

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