Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Reading only specific lines of a database depending on the query name

On a simple website i have some articles that are stored inside my db with a button each which, when pressed, leads the reader to another site with the matching content.

I have two classes: on the first I connect to my database and return the result and in the other class where i read the contents of my db.
Now i want to read only specific lines from my db, when the $_GET["eintrag"] matches the value of the first column of each article so i don’t have to read through all my articles which would may slow down my code if there are a lot of articles:

I want to find the single rows inside the function "leseArtikel()" without reinstating a db connection and all the other variables from the "Datenbank" class

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<?php 

class Datenbank {
    public $connection;
    public $query;
    public $result;

    public function __construct($servername, $dbusername, $dbpassword, $dbname) {
        $this->connection = mysqli_connect($servername, $dbusername, $dbpassword);
        mysqli_select_db($this->connection, $dbname);
    }

    public function getResult(){
        
        $query = "SELECT * FROM protokoll_artikel";
        $result = mysqli_query($this->connection,$query);
        
        return $result;    
    }
    
    public function close()
    {
        mysqli_close($this->connection);   
    }
}

class ProtokollEinträge{
    public function leseAlleArtikel(){
        
        $result = new Datenbank("localhost", "*******", "*******", "*******");
        $result = $result->getResult();
        
        if (mysqli_num_rows($result) > 0) { 
            // OUTPUT DATA OF EACH ROW 
            $row = array();
            
            while($row = mysqli_fetch_assoc($result)) {
                $weeks[$row["KW"]] = $row;  
            }                    
        }
        return $weeks;
    }

    public function leseArtikel($eintragsIds){

        $item = new ProtokollEinträge();
        $item = $item->leseAlleArtikel();
        if (!isset($item[$eintragsIds])){
            return false;
        }
        return $item[$eintragsIds];
    }
} ?>

This is the code where i want to implement only finding the matching line:

$eintragsIds = $_GET["eintrag"];
                    
                    
                    $item = new ProtokollEinträge;
                    $item = $item->leseArtikel($eintragsIds);
                    
                    // EIntrag existiert nicht
                    if ($item === false) {
                        echo nl2br("Dieser Artikel existiert nicht! \n
                        Bitte halten sie sich an die verlinkten Artikel auf der Startseite: \n")?>
                        <br><a href="https://djumla.dev/praktikum/protokoll.php">Startseite</a><?php
                    }
                    else {
                        
                    ?>
                    <div class="woche">
                        <h3><?php echo $item["KW"]; ?>:</h3> <h4><?php echo $item["TITEL"] ;?></h4><br>     
                        <p><?php echo $item["INHALT"];?><br></p><p><?php echo $item["TEXT"];?><br></p>
                        <br><a href="https://djumla.dev/praktikum/protokoll.php">Zurück zur Startseite</a> 
                    </div>
                                
                        
                    <?php  }?>
                  

                </section>    
            </main>
                     
            <aside>
                <section>
                    <div class="bild">
                        <img src="bilder/djumla.png"
                        class="avatar">
                    </div>  
                            
                    <h2>Kontakt</h2>
                    <p><img src="bilder/phone.png" width="30px" height="30px" > ********</p>
                    <p><img src="bilder/Vector.png" width="30px" height="20px"> ********</p>
                    <p><img src="bilder/map-pin.png" width="30px" height="30px"> ********</p>
                </section>

I tried using

$query = "SELECT * FROM protokoll_artikel WHERE KW =" .$eintragsIds; ?> 

where $eintragsIds = $_GET["eintrag"]

>Solution :

public function leseArtikel($eintragsIds) {
    $db = new Datenbank("localhost", "*******", "*******", "*******"); // Create single database connection

    $query = "SELECT * FROM protokoll_artikel WHERE KW = ?"; // Prepared statement with placeholder
    $stmt = mysqli_prepare($db->connection, $query); // Prepare the statement

    mysqli_stmt_bind_param($stmt, "i", $eintragsIds); // Bind the parameter

    mysqli_stmt_execute($stmt); // Execute the prepared statement

    $result = mysqli_stmt_get_result($stmt); // Get the result

    if (mysqli_num_rows($result) > 0) {
      $row = mysqli_fetch_assoc($result); // Fetch the single row
      return $row; // Return the single row as an associative array
    } else {
      return false; // Return false if no row is found
    }

    mysqli_stmt_close($stmt); // Close the prepared statement (recommended)
    $db->close(); // Close the database connection (if needed)
  }

Create a single instance of the Datenbank class within leseArtikel. This avoids redundant connections for each call.

$eintragsIds = $_GET["eintrag"];

$item = new ProtokollEinträge();
$artikel = $item->leseArtikel($eintragsIds);

if ($artikel === false) {
  // ... handle non-existent article
} else {
  // ... display article details using $artikel data
  echo "KW: " . $artikel["KW"];
  echo "TITEL: " . $artikel["TITEL"];
  // ... and so on
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading