I have a question that I have not been able to do, I am showing the number of reports. On my web page, But I had to make a new connection to the database, to be able to show the amount of my reports. That is, I have a connection to the database as follows.
<?php
class Conexion{
public static function Conectar() {
define('servidor', 'localhost');
define('nombre_bd', 'sanandre_cmaps');
define('usuario', 'root');
define('password', '');
$opciones = array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8');
try{
$conexion = new PDO("mysql:host=".servidor."; dbname=".nombre_bd, usuario, password, $opciones);
return $conexion;
}catch (Exception $e){
die("El error de Conexión es: ". $e->getMessage());
}
}
}
What the following code does is show the number of reports that are registered
The problem that I had to make another connection because it didn't show me anything if I included the existing connection.
<?php
php $con = mysqli_connect("localhost", "root", "", "sanandre_cmaps");
$query = "SELECT id FROM reportes WHERE estatus = 1";
$query_run = mysqli_query($con, $query);
$row = mysqli_num_rows($query_run);
echo '<h4> Total: '.$row.'</h4>';
?>
What I want to know is how I can reuse the main connection without having to create a new connection, I don't know if you understand me.
In each class you need from the connection, do a
require_once
; suppose in your class X you declare the private variable connection and from then on you start creating your methods, the first will be the constructor, there you add something like this: