My index.php page wont load

So heres the part of the code that gives me the error:

<?php include("path.php"); include(ROOT_PATH . "/controllers/topics.php"); $posts = array(); $postsTitle = 'Recent Title'; $posts = getPublishedPosts(); $trendingPosts = getPublishedPosts(); if (isset($_GET['t_id'])) { $posts = getPostsByTopic($_GET['t_id']); $topicName = selectOne('topics', ['id' => $_GET['t_id']]); $postsTitle = "Search results for posts under '" . $topicName['name'] . "' topic"; }else if (isset($_POST['search-keyword'])) { $postsTitle = "Search results for '" . $_POST['search-keyword'] . "'"; $posts = searchPosts($_POST['search-keyword']); } else { $posts = getPublishedPosts(); } ?>

i get this error:

Fatal error: Uncaught Error: Call to a member function bind_param() on bool in /home/vol14_8/unaux.com/unaux_26832677/htdocs/database/db.php:20 Stack trace: #0 /home/vol14_8/unaux.com/unaux_26832677/htdocs/database/db.php(154): executeQuery('SELECT p.*, u.u...', Array) #1 /home/vol14_8/unaux.com/unaux_26832677/htdocs/index.php(6): getPublishedPosts() #2 {main} thrown in /home/vol14_8/unaux.com/unaux_26832677/htdocs/database/db.php on line 20

dp.php file:

<?php

session_start();
require('connect.php');

function dd($value) // to be deleted
{
echo "

" , print_r($value, true), "

";
die();
}

function executeQuery($sql, $data)
{
global $conn;

$stmt = $conn->prepare($sql);
$values = array_values($data);
$types = str_repeat('s', count($values));
$stmt->bind_param($types, ...$values);
$stmt->execute();
return $stmt;

}

function selectAll($table, $conditions = [])
{
global $conn;

$sql = "SELECT * FROM $table";
if (empty($conditions)) {
    $stmt = $conn->prepare($sql);
    $stmt->execute();
    $records = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);
    return $records;
} else {
    $i = 0;
    foreach ($conditions as $key => $value) {
        if ($i === 0) {
            $sql = $sql . " WHERE $key=?";
        } else {
            $sql = $sql . " AND $key=?";
        }
        $i++;
    }

    $stmt = executeQuery($sql, $conditions);
    $records = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);
    return $records;
}

}

function selectOne($table, $conditions)
{
global $conn;
$sql = "SELECT * FROM $table";

$i = 0;
foreach ($conditions as $key => $value) {
    if ($i === 0) {
        $sql = $sql . " WHERE $key=?";
    } else {
        $sql = $sql . " AND $key=?";
    }
    $i++;
}

$sql = $sql . " LIMIT 1";
$stmt = executeQuery($sql, $conditions);
$records = $stmt->get_result()->fetch_assoc();
return $records;

}

function create($table, $data)
{
global $conn;
// $sql = "INSERT INTO users SET username=?, admin=?, email=?, password=?"

$sql = "INSERT INTO $table SET ";

$i = 0;
foreach ($data as $key => $value) {
    if ($i === 0) {
        $sql = $sql . " $key=?";
    } else {
        $sql = $sql . ", $key=?";
    }
    $i++;
}

$stmt = executeQuery($sql, $data);
$id = $stmt->insert_id;
return $id;

}

function update($table, $id, $data)
{
global $conn;
// $sql = "UPDATE users SET username=?, admin=?, email=?, password=? WHERE id=?"

$sql = "UPDATE $table SET ";

$i = 0;
foreach ($data as $key => $value) {
    if ($i === 0) {
        $sql = $sql . " $key=?";
    } else {
        $sql = $sql . ", $key=?";
    }
    $i++;
}

$sql = $sql . " WHERE id=?";
$data['id'] = $id;
$stmt = executeQuery($sql, $data);
return $stmt->affected_rows;

}

function delete($table, $id)
{
global $conn;
// $sql = "DELETE FROM users WHERE id=?"

$sql = "DELETE FROM $table WHERE id=?";

$stmt = executeQuery($sql, ['id' => $id]);
return $stmt->affected_rows;

}

function unBan($id)
{
global $conn;
// $sql = "DELETE FROM users WHERE id=?"

$sql = "DELETE FROM banned WHERE user_id=?";

$stmt = executeQuery($sql, ['user_id' => $id]);
return $stmt->affected_rows;

}

//$id = delete('users', 11);
//dd($id);

function getPublishedPosts(){

global $conn;
// SELECT * FROM posts WHERE published=1
$sql = "SELECT p.*, u.username FROM posts AS P JOIN users AS u ON p.user_id=u.id WHERE p.published=?";

$stmt = executeQuery($sql, ['published' => 1]);
$records = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);
return $records;

}

function searchPosts($keyword){

global $conn;

$match = '%' .  $keyword . '%';
// SELECT * FROM posts WHERE published=1
$sql = "SELECT
          p.*, u.username
        FROM posts AS P
        JOIN users AS u
        ON p.user_id=u.id
        WHERE p.published=?
        AND p.title LIKE ? OR p.body LIKE ?";

$stmt = executeQuery($sql, ['published' => 1, 'title' => $match, 'body' => $match]);
$records = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);
return $records;

}

function getPostsByTopic($topic_id){

global $conn;
// SELECT * FROM posts WHERE published=1
$sql = "SELECT p.*, u.username FROM posts AS P JOIN users AS u ON p.user_id=u.id WHERE p.published=? AND topic_id=?";

$stmt = executeQuery($sql, ['published' => 1, 'topic_id' => $topic_id]);
$records = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);
return $records;

}

someone help?

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!