1

I know there are a few topics discussing this but unfortunately i can not get it to work for me. I might be overseeing something.

I got the following code:

function get_kurs($block){
    open_connection();
    global $conn;

    $stmt = $conn->prepare("SELECT FachKürzel FROM `Fach` LEFT JOIN `Stunde` ON Fach.Fachname = Stunde.Fachname WHERE Stunde.Stunde = ?");
    $stmt->bind_param("s", $block);
    $stmt->execute();
    $stmt->bind_result($kuerzel);
    $stmt->fetch();

    print $kuerzel;

    $stmt->close();
    $conn->close();
}

When I try to execute it it gives me the described error. I think it might have something to do with the query but running the exact same query in HeidiSQL on the same Database gives me the result i want.

The connection is working since i can get simple querys to display a result on my website.

Any ideas? Thank you in advance!

Edit: Just in case it's important:

function open_connection(){
    $servername = "XXX.XXX.XXX.XXX";
    $username = "username";
    $password = "password";
    $dbname = "XXXX";

    global $conn;
    $conn = new MySQLi($servername, $username, $password, $dbname);

    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }
}
Dharman
  • 30,962
  • 25
  • 85
  • 135
hypestar
  • 39
  • 1
  • 9

1 Answers1

0

The prepare functions can return the statement object or false. If you get a false that means that there was an error.

Try printing the error with $conn->errno or $conn->error, to have a better understanding of what your issue is.

You can change your code to something like this:

function get_kurs($block){
    open_connection();
    global $conn;

    if($stmt = $conn->prepare("SELECT FachKürzel FROM `Fach` LEFT JOIN `Stunde` ON Fach.Fachname = Stunde.Fachname WHERE Stunde.Stunde = ?")){
        $stmt->bind_param("s", $block);
        $stmt->execute();
        $stmt->bind_result($kuerzel);
        $stmt->fetch();
    }else{
        printf('errno: %d, error: %s', $conn->errno, $conn->error);
        die;
    }

    print $kuerzel;

    $stmt->close();
    $conn->close();
}
Esteban Garcia
  • 2,171
  • 16
  • 24
  • It says that i have an error in my SQL Syntax. But like i said the exact same query is working just fine when executed in HeidiSQL. – hypestar Jan 18 '18 at 11:05
  • Must be because of that special character you have on the column name. Can you try doing this right after you connect to mysql? `$conn->set_charset("utf8");` – Esteban Garcia Jan 18 '18 at 11:07
  • Yes it was the special character. I changed the column names and now its working. Thanks for your hints! – hypestar Jan 18 '18 at 11:09
  • Don't use umlauts in programming or for field names. It will save you some headaches. As a general advice: Write your code in english (names for variables, methods, databases, ...) – common sense Jan 18 '18 at 11:29
  • Just for the community. This error with sqlite3 was shoot because the db file was present but was a 0Kb file and despite the `catch(PDOException $e)` no hints came about the db file behavior. – Robert Aug 18 '19 at 15:15