PHP - error en mi minisistema

 
Vista:

error en mi minisistema

Publicado por Jerson Batista (1 intervención) el 29/10/2019 16:05:57
buenos dias tengo un problema Call to a member function query() on null
esta es la clase
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<?php
 
	class Trabajo
	{
 
		private $dbh;
		private $vendedor;
 
		public function _construct()
		{
 
			$this->dbh=new PDO('mysql:host=localhost;dbname=curso_ventas',"root","");
		}
 
		private function set_names()
		{
			 $this->dbh->query("SET NAMES 'utf8'");
		}
		/*mostrar los vendedores*/
		public function get_vendedores()
		{
			self::set_names();
			$sql = " select
				  v.id_vendedor, v.nombre as vendedor ,
				  s.nombre as supervisor, v.cedula, v.telefono,v.correo
				 from
				 vendedor  as v, supervisor as s
				 where
				 v.id_supervisor = s.id_supervisor
				 order by v.id_supervisor desc";
				 foreach ($this ->dbh->query($sql)as $row)
				  {
				 	$this->vendedor[] =$row;
				 }
				 return $this->vendedor;
				 $this->dbh=null;
		}
	}
 
 
 
?>

y esta es la pagina de donde lo llamo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?php
	require_once  ('clases/class.php');
	$tra = new Trabajo();
?>
 
<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<title>Vendedores</title>
	<link rel="stylesheet" href="dist/css/bootstrap.css">
	<link rel="stylesheet" href="dist/css/bootstrap-theme.css">
</head>
<body>
 
	 <div class="container">
 
     <?php include 'php/menu.php'; ?>
 
 
       <table class="table table-bordered">
		    <tr>
		     <th scope="col">ID</th>
		     <th>Nombre</th>
		     <th>Cédula</th>
		     <th>Teléfono</th>
		     <th>E-mail</th>
		     <th>Supervisor</th>
		     <th>Editar</th>
		     <th>Eliminar</th>
 
		    </tr>
 	 <?php
 	 $datos = $tra ->get_vendedores();
 	 	for($i =0;$i<sizeof($datos);$i++)
 	 	{
 	  ?>
		      <tr>
		     <td>ID</td>
		     <td>Nombre</td>
		     <td>Cédula</td>
		     <td>Teléfono</td>
		     <td>E-mail</td>
		     <td>Supervisor</td>
		     <td><a href="" class="btn-secondary">Editar</a></td>
		     <td><a href="" class="btn-danger">Eliminar</a></td>
 
		    </tr>
		    <?php } ?>
 
		</table>
  		<?php include 'php/footer.php'; ?>
    </div> <!-- /container -->
 
	<script type="text/javascript" src="dist/js/jquery-3.4.1.min.js"></script>
	<script type="text/javascript" src="dist/js/bootstrap.js"></script>
</body>
</html>
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
sin imagen de perfil
Val: 1.071
Bronce
Ha mantenido su posición en PHP (en relación al último mes)
Gráfica de PHP

error en mi minisistema

Publicado por Yamil Bracho (888 intervenciones) el 29/10/2019 16:42:56
1) Siempre chequea errores en las operaciones de BD (y en general)
El constructor deberia ser:

1
2
3
4
5
6
7
public function _construct() {
	try {
		 $this->dbh=new PDO('mysql:host=localhost;dbname=curso_ventas',"root","");
	} catch (\PDOException $e) {
		 throw new \PDOException($e->getMessage(), (int)$e->getCode());
	}
}
2) Inicializa el arreglo para guardar los vendedores
3) La instruccion $this->dbh = null nunca es eejcutada porque esta despues del return.
Depende de la logica que le quieran aplicar, se puede colocar en el destructor de la clase

1
2
3
public function __destruct(){
  $this->dbh = null;
}
Te quedaria entonces

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/*mostrar los vendedores*/
	public function get_vendedores() {
		self::set_names();
 
		$sql = "select v.id_vendedor, v.nombre as vendedor ,
			   s.nombre as supervisor, v.cedula, v.telefono,v.correo
			   from vendedor as v, supervisor as s 
			   where v.id_supervisor = s.id_supervisor
			   order by v.id_supervisor desc";
 
		$this->vendedor = array()
		$stmt = $pdo->query($sql);
		while ($row = $stmt->fetch()) {
			$this->vendedor[] = $row;
		}
 
		return $this->vendedor;
	}
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar