I have been learning from Php in an online course that many may know, taught by Informational Pills. In this topic of connection to BBDDs with classes POO and PDO I have an error that took days without being able to resolve.
This small toy program is about a search engine that goes to a database to request the information of some products depending on the country parameter. There are four files in this small program:
index.php
<?php
require ('devuelve_productos.php');
$pais = $_GET["buscar"];
$productos = new DevuelveProductos(); #Aquí iniciamos todo, es como darle al botón del start
$array_productos = $productos->get_productos($pais);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Conexion con clases POO</title>
</head>
<body>
<?php
foreach($array_productos as $elemento){?>
<table>
<tr>
<td><?php echo $elemento ['CÓDIGOARTÍCULO']?> </td>
<td><?php echo $elemento ['NOMBREARTÍCULO']?> </td>
<td><?php echo $elemento ['SECCIÓN']?> </td>
<td><?php echo $elemento ['PRECIO']?> </td>
<td><?php echo $elemento ['FECHA']?> </td>
<td><?php echo $elemento ['IMPORTADO']?> </td>
<td><?php echo $elemento ['PAÍSDEORIGEN']?> </td>
</tr>
</table>
<br><br><?php
};
?>
returns_products.php
<?php
require 'conexion.php';
class DevuelveProductos extends Conexion{
public function DevuelveProductos(){
parent::__construct(); #Aquí está llamando al cosntructor de la clase padre (o sea del archivo conexion). En conclusión, está conectando a la BBDD
}
public function get_productos($dato){
$sql = 'SELECT * FROM PRODUCTOS WHERE PAÍSDEORIGEN = "' . $dato . '"';
$sentencia = $this->conexion_db->prepare($sql);
$sentencia->execute(array());
$resultado = $sentencia->fetchAll(PDO::FETCH_ASSOC);
$sentencia->closeCursor();
$this->conexion_db = null;
return $resultado;
}
}
?>
conexion.php
<?php
class Conexion{
protected $conexion_db;
public function Conexion(){
try {
$this->conexion_db = new PDO('mysql:host = localhost, db_name =pruebas', 'root', '');
$this->conexion_db->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
$this->conexion_db->exec('SET CHARACTER SET utf8');
return $this->conexion_db;
} catch (Exception $e) {
echo "Error: " . $e->getMessage() . " en la línea: " . $e->getLine();
}finally{
}
}
}
?>
form_search_pays.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Formulario de búsqueda</title>
</head>
<body>
<form action="index.php" method="get">
<label for="buscar">Introduzca país: <input type="text" name="buscar"></label>
<input type="submit" name="enviando" value="¡Dale!">
</form>
</body>
</html>
When the main file is run (form_search_paises.php), it sends a call to index.php so that it returns the results of the search.
when I run the file search_ form_paises.php and I put the country I want to search for, I get this error:
Fatal error: Uncaught PDOException: SQLSTATE [3D000]: Invalid catalog name: 1046 No database selected in C: \ xampp \ htdocs \ Course PHP \ BBDD \ Conexion_BBDD_clases_poo \ using_pdo \ returns_products.php: 18 Stack trace: # 0 C: \ xampp \ htdocs \ Course PHP \ BBDD \ Conexion_BBDD_clases_poo \ using_pdo \ returns_products.php (18): PDOStatement-> execute (Array) # 1 C: \ xampp \ htdocs \ Course PHP \ BBDD \ Conexion_BBDD_clases_poo \ using_pdo \ index.php (8): ReturnsProducts-> get_products ('Espa \ xC3 \ xB1a') # 2 {main} thrown in C: \ xampp \ htdocs \ Course PHP \ BBDD \ Conexion_BBDD_clases_poo \ using_pdo \ returns_products.php on line 18
I have reviewed MILLIONS of times line 18, and as far as I can see there is nothing wrong, I have reviewed MILLIONS of times the name of the database and it is also correct, I would like to know if someone can help me with this problem please.
Thank you very much in advance