Не могу найти ошибку почему функция возвращает false
Не могу найти ошибку из за которой функция getPost возращает false
getPosts работает
В базе всего 2 поста.
index.php
<?php
header('Content-type: json/application');
require_once 'config/connect.php';
require_once 'functions.php';
$q = $_GET['q'];
$params = explode("/", $q);
$type = $params[0];
$id = $params[1];
if($type === 'posts'){
if(isset($id)){
getPost($dbh, $id);
}else{
getPosts($dbh);
}
}
functions.php
<?php
function getPosts($dbh){
$stmt = $dbh->query("SELECT * FROM `posts`");
$post = $stmt->fetchAll(PDO::FETCH_ASSOC);
echo json_encode($post);
}
function getPost($dbh, $id){
$query = "SELECT * FROM `posts` WHERE `id` = ?";
$stmt = $dbh->prepare($query);
$stmt->execute([$id]);
if(!$stmt){
echo 'error';
}else{
$post = $stmt->fetch(PDO::FETCH_ASSOC);
echo json_encode($post);
}
}
.htaccess
RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.+)$ index.php?q=$1 [L,QSA]
connect.php
<?php
try {
$dbh = new PDO('mysql:host=localhost;dbname=api_tester', 'root', 'root');
} catch (PDOException $e) {
print "Error!: " . $e->getMessage();
die();
}
Ответы (1 шт):
Автор решения: govnocoder
→ Ссылка
var_dump($stmt->errorInfo());
Ошибка была в том, что в базе не было поля id, вместо него был post_id