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
<?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"]
Marcus D is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.