Help with select based on SQL criteria, and postgresql

0

I'm trying to make a query with sql under one criterion

Well, let me introduce the code.

 include_once "conexion.php";
    /* Ejecuta una sentencia preparada pasando un array de valores */
    $id = 1;
    $sql = "SELECT * FROM users WHERE  Id = :Id";

    $statement = $pdo->prepare($sql); 
if(!$statement){
     echo "ERROR:".PHP_EOL; 
     print_r($pdo->errorInfo()); 
}else{ 
    $statement->execute([':Id'=> 1]); 
    $result = $statement->fetchAll(); 
    var_dump($result); var_dump($statement); 
}
var_dump($result);

The result does not throw me anything,

And apparently no value is returned to me, but if I try with the name criteria, it returns the value I expect.

I'm suspecting that the problem is in the condition using the id since it does not return anything to me.

I hope your help thanks.

I have removed the single quotes from the sql criterion but they still do not return a value to me.

By testing the value I get with var_dump this returns this:

array (size=0)
  empty

THE CODES ARE ALREADY UPDATED, AND AS SEEN IT CONTINUES WITHOUT WORKING

C:\xampp\htdocs\proyecto\select2.php:19:
array (size=0)
  empty

C:\xampp\htdocs\proyecto\select2.php:19:
object(PDOStatement)[2]
  public 'queryString' => string 'SELECT * FROM users WHERE  Id = :Id' (length=35)

C:\xampp\htdocs\proyecto\select2.php:21:
array (size=0)
  empty


1

Try the recommendation, adding the following in the code.

    
asked by JuanL 04.06.2018 в 01:35
source

1 answer

0

I leave this example fully functional for you to guide and adapt it to your needs

<?php 
    $pdo = new PDO("mysql:host=localhost;dbname=demo", "root", "password");
    /* Ejecuta una sentencia preparada pasando un array de valores */
    $id = 2;
    $sql = "SELECT titulo FROM movies WHERE id = :id";
    $statement = $pdo->prepare($sql);
    $statement->bindParam('id', $id);
    $statement->execute();

    while($fila = $statement->fetch()){
        echo $fila["titulo"];
    }
  

As notes I use the fetch method, but inside the while loop to   access each of the keys / columns of my table directly from my   database.

     

Another detail is that to indicate the values that will be used   dynamically in the query I use bindParam to indicate it; of   so that I leave the method execute without arguments

Also, as notes, pass the value 2 of the id to a variable, with which you can also pass a dynamic value in this way $statement->bindParam('id', $id);

    
answered by 04.06.2018 в 02:26