How do I get a Select with PDO in Php correctly

0

I'm trying to get a select from a database.

Index Code

 <?php
                         @$mostrar=$_POST["mostrar"];
                         if($mostrar){
                           $form=1;
                           $mMatricula=$_POST['mMatricula'];
                           sql :: consultar($mMatricula);

                         }

/*Form*/
                         echo"<form action=index.php#contact method=post>";
                         echo"<table id='table2'><tr>";
                         echo"<th><h3>Matricula de Vehiculo a Mostrar</h3></th>";
                         echo"<td><input required type=text name=mMatricula size=20 value=\"".@$mMatricula."\"></td>";
                         echo"<td><input type=submit name=mostrar value=Buscar>";
                         echo"</td></tr>";
                         echo"</table>";
                         echo"</form>";

                         if(@$form>0){
                           echo"<center><form action=index.php#contact method=post>";
                                     echo "<table id='tabla3'> <tr>";
                                                echo "<th>Matricula</th>";
                                                echo "<th>Marca</th>";
                                                echo "<th>Modelo</th>";
                                                echo "<th>Nombre</th>";
                                                echo "<th>Apellido</th>";
                                                echo "<th>Cedula</th>";
                                                echo "<th>Direccion</th>";
                                                echo "<th>Telefono</th>";
                                                echo "<th>Poblacion</th>";

                                           echo "</tr>";
   /*Posible error en el ciclo*/      while(@$row =  mysqli_fetch_array(@$dev)){
                                        echo"<tr>";
                                            echo "<td>";echo $row['Matricula'];echo" </td>";
                                            echo "<td>";echo $row['Marca'];echo" </td>";
                                            echo "<td>";echo $row['Modelo'];echo"</td>";
                                            echo "<td>";echo $row['Nombre'];echo" </td>";
                                            echo "<td>";echo $row['Apellido'];echo" </td>";
                                            echo "<td>";echo $row['Cedula'];echo" </td>";
                                            echo "<td>";echo $row['Direccion'];echo"</td>";
                                            echo "<td>";echo $row['Telefono'];echo" </td>";
                                            echo "<td>";echo $row['Poblacion'];echo" </td>";

                                        echo"</tr>";
                                    }
                                    echo"</table>";
                                    echo"</form></center>   ";
                         }

                         ?>

Code pdo sql.php

<?php
require 'BaseDatos.php';

class sql
{

  function __construct()
  {
    # code...
  } 
 public static function consultar($mMatricula){
    try {
      $pdo = BaseDatos::obtenerBD()->obtenerConexion();
       $comando ="SELECT * FROM vehiculos WHERE Matricula = ?";
       $sentencia = BaseDatos::obtenerBD()->obtenerConexion()->prepare($comando);
       $sentencia->execute();//Obtiene solo datos no envia
       return $sentencia->fetchAll();


    } catch(PDOException $e) { return $e;}
  }

}
?>

Codigo datosconexion.php (for siacaso, this if it runs well)

<?php
define('HOSTNAME', 'localhost');
define('DATABASE', 'transito');
define('USERNAME', 'root');
define('PASSWORD', '');
?>

Basecode Data.php // Same runs nice

<?php require_once 'datosconexion.php';
/**
*
*/
class BaseDatos{
    private static $bd=null;
    private static $pdo;

    function __construct()
    {
        try {
            self::obtenerConexion();
        }catch(PDOException $e) { }
    }

    public static function obtenerBD() {
     if(self::$bd==null) {
        self::$bd = new self();
     }
     return self::$bd;
    }

    public function obtenerConexion() {
         if(self::$pdo==null) {
            self::$pdo= new PDO('mysql:dbname='.DATABASE.';host='.HOSTNAME.';',USERNAME,PASSWORD, array(PDO::MYSQL_ATTR_INIT_COMMAND=>"SET NAMES utf8"));
         self::$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
         }
         return self::$pdo;
    }

    function __destruct() {
        self::$pdo=null;

    }

}

?>
    
asked by inusui 24.11.2018 в 07:10
source

2 answers

1

As simple as @$mostrar=$_POST["mostrar"]; if($mostrar){ $form=1; $mMatricula=$_POST['mMatricula']; $data= sql :: consultar($mMatricula); foreach($data as $key => $value){ echo $value["id"]; } }

    
answered by 24.11.2018 в 08:56
0

Where the call to class sql is found :: consult ($ mMedicine);

 list($mNombre, $mMarca, $mModelo, $mNombre, $mApellido, $mCedula, $mDireccion, $mTelefono, $mPoblacion)= sql :: consultar($mMatricula);

Now remove the While and add the variables placed in the list

  echo"<tr>";
                                            echo "<td>";echo $mNombre;echo" </td>";
                                            echo "<td>";echo $mMarca;echo" </td>";
                                            echo "<td>";echo $mModelo;echo"</td>";
                                            echo "<td>";echo $mNombre;echo" </td>";
                                            echo "<td>";echo $mApellido;echo" </td>";
                                            echo "<td>";echo $mCedula;echo" </td>";
                                            echo "<td>";echo $mDireccion;echo"</td>";
                                            echo "<td>";echo $mTelefono;echo" </td>";
                                            echo "<td>";echo $mPoblacion;echo" </td>";

                                        echo"</tr>";
  

source List Manual Php   And the sql.php   send data with

   $sentencia->execute(array($mMatricula));

and return them

   return $arr= $sentencia->fetch();
     

Source PHP PDO Prepared Statements Tutorial to Prevent SQL Injection

    
answered by 24.11.2018 в 08:36