0

I am new here. I am having problems with my site that I am developing. Could you help me how to fix it.

Error -

Fatal error: Uncaught Error: Call to undefined function dbconnect() in /storage/ssd4/448/8893448/public_html/blog/index.php:5 Stack trace: #0 {main} thrown in /storage/ssd4/448/8893448/public_html/blog/index.php on line 5

Here's connection .php

<?php

class connection{

    public $db_host = 'localhost';
    public $db_name = 'id8893448_ifians';
    public $db_user = 'id8893448_ifdb';
    public $db_pass = 'admin';

    public function dbconnect()
    {
        try{
            $conn = new PDO("mysql:host=".$this->db_host.";dbname=".$this->db_name,$this->db_user,$this->db_pass);
           $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        }
        catch(PDOException $e)
        {
            echo 'ERROR: ' . $e->getMessage();
        }    
        return $this->conn;
    }
}

?>

and index.php

<?php

include 'functions/connect.php';
include 'functions/queries.php';
$conx = dbconnect();
$recentArray = Array();
$recent = recents() ;
$resultRecent = mysqli_query($conx, $recent);
$blogPost = Array();
while($rowRecent = mysqli_fetch_assoc($resultRecent)) {
    array_push($recentArray, $rowRecent);
};

//var_dump($recentArray);



$selectTitle = selectTitle();
//print_r($selectTitle);

$selectAuthor = selectAuthor();
$selectDate = selectDate();

$selectTitleArray = Array();
$selectAuthorArray = Array();
$selectDateArray = Array();

$selectTitleResult = mysqli_query($conx, $selectTitle);
$selectAuthorResult = mysqli_query($conx, $selectAuthor);
$selectDateResult = mysqli_query($conx, $selectDate);

while($selectTitleRow = mysqli_fetch_assoc($selectTitleResult)) 
{
    array_push($selectTitleArray, $selectTitleRow);
};

while($selectAuthorRow = mysqli_fetch_assoc($selectAuthorResult)) 
{
    array_push($selectAuthorArray, $selectAuthorRow);
};

while($selectDateRow = mysqli_fetch_assoc($selectDateResult)) 
{
    array_push($selectDateArray, $selectDateRow);
};
//print_r($selectTitleArray);






if (isset($_GET['query']))
{
    $blog = modifiedQuery($_GET['query'],$_GET['value']);
}

elseif (isset($_GET['title']) || isset($_GET['author']) || isset($_GET['created']))
{
    $blog = submitQuery($_GET['title'],$_GET['author'],$_GET['created']);
}
else 
{   
        $blog = originalQuery();
}

//print_r($blog);

$result = mysqli_query($conx, $blog);

//print_r($result);

while($row = mysqli_fetch_assoc($result)) 
{
    array_push($blogPost, $row);
};



include 'templates/template.php';


?>
Mht K
  • 9
  • 4

1 Answers1

1

You need to create an instance of your connection class which you may then use to call its dbconnect function

$newcon = new connection();
$conx = $newcon->dbconnect();
Dave
  • 5,108
  • 16
  • 30
  • 40
  • 1
    There really is no point creating a local variable `$conx = (new connection)->dbconnect()` is enough. Just saying. OR you can add that function in and have it do the same, but with a return (obviously). – ArtisticPhoenix Mar 05 '19 at 14:15
  • True enough but if there are other functions `$newcon` could be useful. – Dave Mar 05 '19 at 14:16