Codeaur Codeaur - 1 month ago 6
PHP Question

Fatal error: Call to undefined method mysqli_result::format()

I know that the error says that format isn't a valid code, but I used it before and it worked fine. So I hope someone knows a code to replace it with or now how to fix it.

require_once 'config.php';

$checker = "SELECT StartTime FROM ap21_Teachers WHERE Mentor_Class = 'I2C'";
$checker2 = "SELECT EndTime FROM ap21_Teachers WHERE Mentor_Class = 'I2C'";
$checker3 = "SELECT Minutes FROM ap21_Teachers WHERE Mentor_Class = 'I2C'";

$result1 = mysqli_query($mysqli, $checker);
$result2 = mysqli_query($mysqli, $checker2);
$minutes = mysqli_query($mysqli, $checker3);

$starttime = new DateTime($result1);
$endtime = new DateTime($result2);

while($starttime <= $endtime)
{

echo "<option value='".$starttime->format('H:i:s')."'>".$starttime->format('H:i:s')."</option>";

$starttime = date_add($starttime, date_interval_create_from_date_string($minutes, ' min'));
}

echo " </select>";


and above I grab the vars out of the database

Answer

The problem is mysqli_query returns a mysqli_result which you need to read before using:

require_once 'config.php';

$checker = "SELECT StartTime, EndTime, Minutes FROM ap21_Teachers WHERE Mentor_Class = 'I2C'";

$result = mysqli_query($mysqli, $checker);      //Execute query

 if (!$result) { //Result may be false if there's an error
     echo "Error getting result";
 }
 $row = mysqli_fetch_assoc($result); //Put first result in an array
 if (!$row) {  // $row will be false if there's no result
     echo "No data found";
 }

 //Notice how the array entry keys match the columns in the query.     
 $starttime = new DateTime($row["StartTime"]);
 $endtime = new DateTime($row["EndTime"]);         
 $minutes = $row["Minutes"];

while($starttime <= $endtime)
{

     echo "<option value='".$starttime->format('H:i:s')."'>".$starttime->format('H:i:s')."</option>";

     $starttime = date_add($starttime, date_interval_create_from_date_string($minutes, ' min'));
}

echo " </select>";
Comments