Answer the question
In order to leave comments, you need to log in
Pulling data from db using pdo?
I swim in php for about a month and a half, I used to do it through mysqli, but I found out that it’s better through PDO
. So the problem is that I can’t figure out how to display data from the database.
In the database table
id record 1
login record admin
code
$pdo = new PDO("mysql:dbname=user;host=localhost", "root", "");
$sql = $pdo->prepare('SELECT * FROM use WHERE login = :login');
$sql->execute([':login' => $login]);
$data = $sql->fetch(PDO::FETCH_ASSOC);
print $data;
Answer the question
In order to leave comments, you need to log in
How do I use PDO. I create a dbconnection.php file and put the following code
$host = 'localhost';
$database = 'dbname';
$user = 'user';
$pass = 'root';
$dsn = "mysql:host=$host;dbname=$database;";
$options = array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
);
$pdo = new PDO($dsn, $user, $pass, $options);
function user() {
global $pdo;
$stmt = $pdo->query('SELECT * FROM users');
$data = $stmt->fetchAll();
return $data;
}
$datas = user();
var_dump($datas);
$stmt = $pdo->query('SELECT * FROM users');
$data = $stmt->fetchAll();
turn on all the logs first, maybe there is no connection to the database and the use word is reserved. try to `screen` all fields and table names
ps. through PDO is not better. she is more blunt. it is better to write your own wrapper and inject your own specific driver into it.
Try like this
$query = $this->db->prepare("SELECT * FROM model WHERE id=:cat_id");
$query->execute(array('cat_id'=>$cat_id));
return $query->fetchAll();
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question