When I load the PHP page containing the code below I get the error “Call to a member function fetch_object() on a non-object.” It pertains to the line starting with the word while. Why is this error popping up? Ignore my security weaknesses please.
PHP snippet:
header('Content-Type:application/json;charset=utf-8');
$file_absolute = "---placeholder for correct file path---";
include_once($file_absolute);
$mysql = new mysqli($db_host, $db_username, $db_password, $db_name);
$verb_value = $_POST['verb_value'];
$mysql->query("SET CHARACTER SET 'utf8'");
$result = $mysql->query("SELECT present_tense FROM $verb_value");
$queryResult = array();
while ($row = $result->fetch_object())
{
$queryResult[] = $row->present_tense;
}
You are lacking error checking in your code:
The result of your query is a non object, but you did not check for that.
Note: your code is prone to SQL Injection:
$verb_value = $_POST['verb_value']results in a possibility to inject SQL code into the database without checking!