-1
Code suggestion:
<?php
$dsn = 'mysql:host=localhost;dbname=dbphp7';
$user = 'root';
$password = '';
try{
$conn = new PDO($dsn, $user, $password);
}
catch(PDOException $ex){
echo 'Connection failed: ' . $e->getMessage();
}
using the Fetchall
$stmt = $conn->prepare("SELECT * FROM tb_usuarios ORDER BY deslogin");
$stmt->execute();
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
using the Fetch
$stmt = $conn->prepare("SELECT * FROM tb_usuarios ORDER BY deslogin");
$stmt->execute();
while ($linha = $stmt->fetch(PDO::FETCH_ASSOC)) {
var_dump($linha);
}
Notice the difference between them.
The syntax of your dsn is wrong, is "mysql:host=localhost;dbname=dbphp7", and not in this way that you did
– Everton Neri
@jonas2591 It will probably return only the first record found in the table according to the criteria passed in the query, what is happening??
– Oliveira
@jonas2591 a suggestion to be able to answer you faster about some doubt and put the code in the question, in this you put the code print.
– Oliveira
I know this code, it’s from a video lesson of the Hcode Full PHP Course, if you go back to video lesson and pay attention to the code you’ll see where you’re going wrong.
– Laércio Lopes