As mentioned in comments, this is a scoping issue. Specifically, $con
is not in scope within your getPosts
function.
You should pass your connection object in as a dependency, eg
function getPosts(mysqli $con) {
// etc
I would also highly recommend halting execution if your connection fails or if errors occur. Something like this should suffice
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); // throw exceptions
$con=mysqli_connect("localhost","xxxx","xxxx","xxxxx");
getPosts($con);
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…