0

I wanted to make a login script for my program but its not working. The error is on the bind_param. Can you please explain why it's not working and teach me how to do it right?

Code:

<?php
$username = $_GET['username'];
$key = $_GET['key'];
$hwid = $_GET['hwid'];


$istKorrekt = istKorrekterSchluessel($username, $key, $hwid);
if($istKorrekt) {
    echo 'true';
} else {
    echo 'false';
}

mysqlTrennen();


function mysqlVerbinden() {
    global $mysqlVerbindung;
$mysqlHost = "localhost";
$mysqlBenutzer = "ts3botauth";
$mysqlPasswort = "nope";
$mysqlDatenbank = "ts3botauth";
$mysqlTabelle = "ts3botauth";
$mysqlVerbindung = new mysqli($mysqlHost, $mysqlBenutzer, $mysqlPasswort, $mysqlDatenbank);

    if($mysqlVerbindung->connect_errno)
        return false;

    return true;
}

function mysqlTrennen() {
    global $mysqlVerbindung;

    $mysqlVerbindung->close();
}

function istKorrekterSchluessel($username, $key , $hwid) {
    global $mysqlVerbindung;
    $mysqlTabelle = "ts3botauth";
    $stmtPruefung = $mysqlVerbindung->prepare("SELECT EXISTS(SELECT * FROM " . $mysqlTabelle . " WHERE `Key`=? AND `Username`=? AND `HWID`=?) AS schluesselKorrekt");

    if(!$stmtPruefung) {
        return false;}  
   $stmtPruefung->bind_param("s",$username);
    $stmtPruefung->bind_param("s", $key);
    $stmtPruefung->bind_param("s", $hwid);

    $stmtPruefung->execute();

    $stmtPruefung->bind_result($schluesselKorrekt);

    $stmtPruefung->fetch();

    return ($schluesselKorrekt == 1);
}

?>

Don't Panic
  • 41,125
  • 10
  • 61
  • 80

1 Answers1

2

That's not how bind_param works in mysqli. Maybe you were thinking of PDO? In mysqli you have to bind them all at once with one statement.

$stmtPruefung->bind_param("sss",$username, $key, $hwid);

Line 64 will be $stmtPruefung->bind_param("s",$username);, and you're getting that "Number of variables doesn't match" error because it's expecting all three and you're giving it one.

Don't Panic
  • 41,125
  • 10
  • 61
  • 80
  • *"because it's expecting all three and you're giving it one."* - which is what [I said](http://stackoverflow.com/questions/43211976/mysqli-stmtbind-param-number-of-variables-doesnt-match-number-of-parameter#comment73495801_43211976) in so many words. ;-) – Funk Forty Niner Apr 04 '17 at 15:59
  • @Fred-ii- well, sort of. I'm saying it never even makes it to `$stmtPruefung->bind_param("s", $hwid);` because it fails on `$stmtPruefung->bind_param("s",$username);`. – Don't Panic Apr 04 '17 at 16:02
  • Yeah I know. Maybe it's just the way my mind works ;-) but we basically said the same thing, only differently. But that works. As you mentioned in your answer; maybe the OP thought that the PDO method of executing was the same as MySQLi_, which is not. – Funk Forty Niner Apr 04 '17 at 16:03