Execute query without foreach

1

How do I run this code without foreach, it is completely empty and is only serving to give value to the attributes, so it is not listing anything, and I wanted to leave the code cleaner and cleaner, so I wanted to know how to do it execute the line $con->query($sql, PDO::FETCH_ASSOC) as $row "without needing foreach .

$con = new PDO("mysql:host=localhost; dbname=estagio;charset=utf8", "root", "");
$sql = "SELECT * FROM clientes WHERE id_cliente = '$_SESSION[id]'";
foreach($con->query($sql, PDO::FETCH_ASSOC) as $row){
}
    
asked by anonymous 31.03.2018 / 01:49

1 answer

1

It would look like this:

$var = $con->query($sql, PDO::FETCH_ASSOC);
print_r($var);

But if you have more than 1 record, it will only bring the last.

The repetition is used to read row by line, and storing in the variable that will be used later. So if you do not do this, it will overlap your variable (in this case $var ).

    
31.03.2018 / 02:32