In mysql I have this code below:
$row = mysql_fetch_assoc($query);
$dbactive = $row['active'];
if ($dbactive == 1){
...
}...
But I have to use mysqli in my project. So I have tried changing it below but I am not quite getting it right. Can anyone just help me correct the below statement in mysqli so it matches the mysql’s statement above?
UPDATE:
My attempt in mysqli:
// don't use $mysqli->prepare here
$query = "SELECT TeacherForename, TeacherSurname, TeacherUsername, TeacherPassword, Active FROM Teacher WHERE TeacherUsername = ? AND TeacherPassword = ? LIMIT 1";
// prepare query
$stmt=$mysqli->prepare($query);
// You only need to call bind_param once
$stmt->bind_param("ss",$teacherusername,$teacherpassword);
// execute query
$stmt->execute();
// get result and assign variables (prefix with db)
$stmt->bind_result($dbTeacherForename,$dbTeacherSurname,$dbTeacherUsername,$dbTeacherPassword, $dbActive);
while($stmt->fetch()) {
if ($teacherusername == $dbTeacherUsername && $teacherpassword == $dbTeacherPassword) {
if ($dbActive == 1){
$loggedIn = true;
}
}
}
If you’re only returning one row, you don’t need a
whileloop.bind_result()takesxcolumns and assigns their values, in order, to the variables you pass in. So, assuming that your result only contains one row with one column,bind_result($dbactive)would be the same as$dbactive = $row['active']withmysql_*.