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);
}
}
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();
}