Fatal error: Call to a member function execute() on boolean in PHP

1
(2.7K Views)

×

Please Login or Register to process.

I am creating a Blog system in PHP with an MYSQL database. I have used a tutorial "Create a blog using PHP and MYSQL database". I am getting an error "Fatal error: Call to a member function execute() on boolean in C:\xampp\htdocs\newssite\amp\article.php on line 6" . I have tried many solutions. I am tired of how to fix this "Fatal error: Call to a member function execute() on boolean" error in PHP. 

I am attaching my PHP source code -

<?php include 'header.php' ?>

<div class="content-div">

                      <?php

                        include "config.php";

                 $sql = $connect->prepare('SELECT id, content, title, title_slug, image_url, created_at FROM posts WHERE title_slug = :title_slug');

                 $sql->execute(array(':title_slug' => $_GET['id']));

                        $result = mysqli_query($connect, $sql) or die("Query Failed.");

                        if(mysqli_num_rows($result) > 0){

                          ($row = mysqli_fetch_assoc($result))

                      ?>

                        <h2><?php echo $row['title']; ?></h2>

                        <amp-img src="<?php echo $row['image_url']; ?>" alt="Welcome"  height="200" width="330"></amp-img>

                         <p><?php echo $row['content']; ?></p>

                        <?php

                          }

                        else{

                          echo "<h2>No Record Found.</h2>";

                        }

                        ?>

                    </div>  

Kindly fix this issue. 

asked 24 Apr 2020 in PHP by liveatenws(210 Points)

Share

2 Answers
answered 24 Apr 2020 by vishalrana1(7.3K Points) (edited)
1

The main possibility of your issue is the connection file. I think there is an issue with your connection (config code ). Where is your config file code? 

Check your connection parameters and make sure to correct them. If you will get this "

Fatal error: Call to a member function execute() on boolean "  error again then, edit your question description. Add your connection file code also. 


Comment

answered 25 Apr 2020 by shakti(9.1K Points) (edited)
1

The errors help to improve coding skills. You are getting a fatal error: Call to a member function.   I am tried your code and reviewed Create a blog using PHP and MYSQL database the article link. I think you are getting an issue with your connection file. 

define('DBHOST','localhost');

define('DBUSER','root');

define('DBPASS','');

define('DBNAME','db_name');

$db = new PDO("mysql:host=".DBHOST.";dbname=".DBNAME, DBUSER, DBPASS);

$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

 $user = new User($db); 

//set timezone- Asia Kolkata 

date_default_timezone_set('Asia/Kolkata');

$user = new User($db); 

 

You should check your connection details. If you have any other issues, comment here. 


Comment

Related Questions :-

Your Answer

You can post your answer after login..