Utilizing object-oriented programming to establish a straightforward problem tracking system

In this code snippet, we have dbconnect.class.php:

<?php  

class Connect 
{ 
    public $error; 
    protected $db; 
   
    public function __construct() 
    { 
        $link = mysql_connect("localhost","root","1") or $this->error = mysql_error(); 
        $db = mysql_select_db("tarih",$link); 
        $this->db = $db; 
    } 
} 

?>

And in the main PHP file:

<?php
//header.class.php 
require_once 'dbconnect.class.php'; 

class Header extends Connect 
{ 
    public $headers = array(); 
    private $baglan; 
    
    public function __construct() 
    { 
        /* 
        * This class processes the header information for pages. 
        */ 
        $baglan = $this->db; 
    } 

    public function sayfaHeader($sayfa = true) 
    { 
        $sql = "SELECT * FROM header WHERE id='" . $sayfa . "'"; 
        $query = mysql_query($sql,$this->baglan); 
    } 


} 

Header::sayfaHeader(); 
?>

However, upon running the main PHP file, an error occurs:

Fatal error: Using $this when not in object context in C:\AppServ\www\ilk\class\header.class.php on line 19  

The issue seems to be on line 19:

$query = mysql_query($sql,$this->baglan);  

What could be causing this problem? It's challenging for me to pinpoint since I haven't worked with PHP code for quite some time.

Answer №1

Header::sayfaHeader(); 

To call a method without creating an object, you can use the following syntax:

Header obj = new Header();
obj->sayfaHeader();

If you want to call a class's method, declare the method as static. Note that static methods and members do not have a reference to the class object ($this).

UPDATE:
The mysql_select_db function returns a bool value. Make sure to use your $link variable for querying.

Answer №2

It seems like the problem lies in your code rather than the database.

The issue is that you are calling a static method without an instance.

Header::sayfaHeader(); 

This particular method requires an instance to be called properly. You can fix it by creating an object and then calling the method on that object.

Here's how you can modify your code:

Header obj = new Header();
obj->sayfaHeader();

Answer №3

Constructing the parent is necessary to initialize the database class

public function __construct() 
{ 
    parent::__construct();
    $connection = $this->db; 
} 

Answer №4

class DatabaseConnection 
{ 
    // Making a constructor function
    public function __construct() 
    { 
        // Assigning $this->db to $this->baglan for easier access within the class
        $this->baglan = $this->db; 
    } 

    // Function to retrieve page header information based on ID
    public function getPageHeader($pageId = true) 
    { 
        $sql = "SELECT * FROM header WHERE id='" . $pageId . "'"; 

        // Executing query using inherited database connection ($this->db)
        $query = mysql_query($sql, $this->db); 
    } 
} 

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Unable to Preview jQuery Video in Div After PHP Validation, But Image Preview Works Fine

Successfully utilizing the code below (with variations in IDs, etc.), I am able to preview a banner image in a div after PHP validates the upload using AJAX. This process is working perfectly without any issues. However, I am encountering an issue when at ...

While the MySQL syntax error is causing issues in the PHP application, it runs smoothly in the MySQL

UPDATE: Check out the 'solution' in my comment below I am currently executing an AJAX POST request to my backend PHP, where form data is transmitted to a dedicated CRUD PHP script that is responsible for creating a new record. The SQL syntax I ...

Effective ways to protect your SMS account verification from potential DDoS attacks

One of the features of my app is user registration, where users input their mobile phone numbers and other relevant data. To ensure the validity of a user before saving their information to our database, we send an SMS containing a verification code that m ...

The PHP do loop seems to have trouble exiting

I've created a script that randomly redirects to one of 8 pages after narrowing down choices from the database. However, I encountered an issue with the do-while loop. Below is the code snippet: <?php $surveys = array( 1 => "711275", ...

Refresh the table data dynamically without the need for page reloading

Looking for a way to update data in a table without refreshing the entire page? Check out this example: Here is the form: https://i.stack.imgur.com/ISQbC.png After clicking on Update All, only the first row gets updated, not the subsequent rows. <?ph ...

What is the best way to store an array of file names in a single column in a database table?

I have a table named t1 with two columns: id and name. I want to insert file names in the name column. <input type="file" name="files[]" multiple></br> <input type="submit" name="submit" value="ADD"> Table: id | name 1 | e1.jpg, e2 ...

How can I create a dropdown menu that is dependent on another dropdown menu using Ajax in my Laravel application?

I have two dropdown fields that are dependent on each other - Class & Section. I am trying to Select * from sections where class_id=selected Class Id. Although I attempted to achieve this using java script, it doesn't seem to work for me. Here are ...

Increasing the value of a string in PHP

Looking to generate an array of strings that start with a random amount of letters (0 to more than 10) and end with numbers. For example: - S001, S002, ..., S015 - AB67116, AB67117, ..., AB67128 - G07, G08, G09, G10, G11 If given the starting string an ...

MySQL Node error - Error: Unable to add Quit after calling quit command

I am in the process of developing a promise-based API. The functionality works smoothly when handling one request at a time. However, I encounter an error if two or more requests reach the server within a second of each other, displaying the following mess ...

Curl malfunction: No response received from server

I am facing issues while attempting a CURL Post. My task involves posting a list of zip codes to an API, and for certain selections, the list can potentially be quite long. The process works flawlessly when I post only a few zip codes. However, when I inc ...

Scraping Websites with SimpleHTMLDom in PHP

I am struggling to extract specific data from a table on a website page, particularly the columns name, level, and experience. The table's alternating row colors (zebra pattern) are complicating this task. I have implemented SimpleHTMLDom for this pu ...

Convert the PHP variable into a JSON object

i am currently working on a project with Codeigniter below is my PHP switch-case section case 'check': $balance = $this->Model_transactions->getUserBalance($this->input->post('userId')); $needToPay = floatval($this->inp ...

Using PHP to send post requests with Ajax for DataTables functionality

I'm currently working on populating a datatable using datatables.net. There's a specific field that triggers an AJAX post request after it's changed. Here is the JavaScript code: jQuery(document).ready(function() { if(window.location.h ...

Execute PHP code upon JavaScript confirmation

Whenever I update the status, an email is automatically sent. The issue is that it sends the email again if any other field is updated as well. What I want is a confirmation prompt before sending the email. I tried using AJAX to handle this, but even thoug ...

Keep only the PHP characters intact while replacing all others in the given string

I need help with extracting the year 1988 from a string that says "Trade Card Catalogue 1988 Edition". Is there a more efficient solution instead of using an array, str_replace, and trim? $string = 'Trade Card Catalogue 1988 Edition'; $letters = ...

Need to retrieve precise information from a website?

Struggling to extract information from this specific section of a webpage <div id="menu_pannel"> <ul class="sf-menu" id="nav"> <li class="current"><a href="/" class="current" >Home</a></li> <li class=""> ...

Sending a request to a PHP script using AJAX to navigate to a different webpage

I'm currently working on developing a login script (still in the early stages, I plan to look into password encryption later). However, I'm facing an issue with redirecting to a specific page. Presently, I have two possibilities - an admin or a ...

The HTML head JavaScript files are not applicable to dynamic Bootstrap modals

I have broken down my PHP files into smaller parts, for example, tillbody.php contains everything from the HTML tag to the body tag, followed by navbar.php and so on. I am including these files in index.php. Here is the code for a better understanding: &l ...

Creating a customized navigation menu for websites containing subdirectories: Tips and techniques

I'm eager to grasp how navigation functions on websites with subdirectories. For instance, if my layout looks like this, \index.php \about_us\about_us.php The navigation link for index.php to return to the homepage would be, ...

Encountering the error code 'ERR_EMPTY_RESPONSE' while utilizing an AJAX-powered live search feature

My website features a live AJAX search bar that retrieves records from a MySQL database. However, when users repeatedly conduct searches by modifying the search criteria, some web browsers display an error message stating 'ERR_EMPTY_RESPONSE'. ...