1

Не могу найти ошибку из за которой функция getPost возращает false

getPosts работает

https://i.stack.imgur.com/SkXcE.jpg

В базе всего 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 Answers1

1

var_dump($stmt->errorInfo()); Ошибка была в том, что в базе не было поля id, вместо него был post_id