You have two errors depending on what type of connection you use.
The first error indicates that the connection to the database is not established correctly for some reason and for lack of error detection code you are not aware of it, leaving the variable false
at% value% or the consultation is not done for some reason and you are not aware of it either.
The second ( $conexion
) is caused by defining the function Fatal error: Cannot redeclare conectar()
in two files. Check your files well so as not to repeat the definition of that function in several points.
Edited to show you a way to get the job done without using global variables.
Create a file that is named conectar()
with this content:
<?php
/**
* Establece la conexión con la base de datos y finaliza la ejecución
* en caso de error.
*
* @return resource Recurso asociado a la conexión MySQLi
*/
function conectar() {
$conexion = new mysqli('localhost', 'root', '', 'vasco');
/* Presuponemos PHP >= 5.3 si usas fetch_all */
if ($conexion->connect_error !== null) {
die($conexion->connect_error);
}
if ($conexion->set_charset('utf8') === false) {
die($conexion->error);
}
return $conexion;
}
/**
* Cierre la conexión MySQLi y devuelve el resultado.
*
* @param resource $conexion Recurso de conexión MySQLi
*
* @return boolean Estado del cierre de la conexión
*/
function desconectar($conexion) {
/* Forma correcta de cerrar la conexión */
return $conexion->close();
}
This way, if you have connection problems, they will appear on the screen and stop the execution of the rest of your application.
And this correct way to use the previous file:
/* Incluimos una única vez la definición de funciones de MySQLi */
require_once 'funciones.bd.php';
/* Forma de realizar el trabajo */
$conexion = conectar();
/* Obtenemos los periodos introduciendo como parámetro la conexión MySQLi */
$periodos = muestraPeriodos($conexion);
/* A partir de aquí podemos trabajar con los datos de periodos */
/* ... */
/* Tras terminar el trabajo con la base de datos cerramos la conexión */
desconectar($conexion);
/**
* Obtiene todos los registros de periodos.
*
* @param resource $conexion Recurso de conexión MySQLi
*
* @return array Matriz asociativa con todos los registros
*/
function muestraPeriodos($conexion) {
/* Realizamos la consulta SQL */
$respuesta = $conexion->query("SELECT * FROM periodos");
/* En caso de error terminamos (tabla no existente, por ejemplo) */
if ($respuesta === false) {
die($conexion->error);
}
/* Obtenemos los datos y liberamos la memoria asociada al resultado */
$resultado = $respuesta->fetch_all(MYSQLI_ASSOC);
$respuesta->free();
/* Una vez liberados los recursos podemos devolver el resultado */
return $resultado;
}
This way, only the definition of the connection and disconnection functions to your database will be included once. You will also know what happened with the query and the execution of your application will not continue in case of error.
I have eliminated the need to maintain a global variable called funciones.bd.php
, but as you can see in the example, you will have to make calls to your functions by sending as a parameter the resource of the connection.
I hope this solves your problems.