It had not happened to me until I wanted to make a chat with php and ajax, I know that a chat is better to do it with other languages, but it does not demand the robustness to use the other languages.
My query that I am trying to do is the following, this is just to make a notification type where it shows the last message the person has received and prints it on screen
function obtener_mensajes($conexion, $us) {
$statement = $conexion->prepare("SELECT SQL_CALC_FOUND_ROWS m.idEmitter, m.nombre, m.message, m.sent, m.idReceiver
FROM messages m
INNER JOIN (SELECT idEmitter, idReceiver, max(idChat) AS idChat
FROM messages
WHERE seenEmpresa = 0
GROUP BY idEmitter, idReceiver) mm
ON m.idChat = mm.idChat
AND m.idEmitter = mm.idEmitter
AND m.idReceiver = mm.idReceiver
WHERE m.idReceiver = $us
AND seenEmpresa = 0");
$statement->execute();
return $statement->fetchAll();
}
@$emit = obtener_mensajes($conexion, $us);
The error is generated by the variable $ emit
The error that appears on the screen is the following:
Fatal error: Uncaught Error: Call to a member function prepare () on null in C: \ xampp \ htdocs \ maybe company \ functions.php: 111 Stack trace:
0 C: \ xampp \ htdocs \ maybe company \ functions.php (126): get_messages (NULL, '3') # 1 C: \ xampp \ htdocs \ maybe
company \ index.php (4): require ('C: \ xampp \ htdocs ...') # 2 {main} thrown in C: \ xampp \ htdocs \ maybe company \ functions.php on line 111
In line 111 that you mention is the line of the preparation of my query
This is the result that should appear, since it is the one that appears in my database manager HeidiSQL
Calling the connection function
function conexion($bd_config) {
try {
$conexion = new PDO('mysql:host=localhost;dbname=' . $bd_config['basedatos'], $bd_config['usuario'], $bd_config['pass']);
return $conexion;
} catch (PDOException $e) {
return false;
}
}
$ bd_config comes from another file
$bd_config = array(
'basedatos' => 'nombre',
'usuario' => 'nombre',
'pass' => 'pass',
);