I’m newish to PHP and OOP and I have been using the PDO class to connect and retrive info from my MYSQL database. Below is and excerpt of the files in question:
lyrics.class.php
<?php
class lyrics{
public $con;
public $kill;
public function __construct(connection $con){
$this->con = $con->con;
}
public function getLyricsFromURL($lyricsid){
$results;
$getlyrics = $this->con->prepare("SELECT * FROM `lyrics` WHERE lyrics_id = :lyricsid");
$getlyrics->bindParam(':lyricsid',$lyricsid,PDO::PARAM_INT);
$getlyrics->execute();
$results = $getlyrics->fetchAll(PDO::FETCH_OBJ);
foreach ($results as $result) {
echo $result->lyrics_content;
}
}
index.php:
<?php
include 'inc/header.php';
include 'libs/connection.class.php';
include 'libs/lyrics.class.php';
$conn = new connection();
$lyrics = new lyrics($conn);
$lyrics->getLyricsFromURL(55);
?>
<h1><?php echo $lyrics->result->lyrics_title;?></h1>
OTHERS:
there is a connection class in another file but i won’t show it here, just know that the connection works.
The problem:*
The problem I’m having is i want to echo out individual column of results from the **getLyricsFromURL() method in the lyrics class, I have tried but it doesn’t work.
What is the correct way to do this?
Thanks in advance.
The method
getLyricsFromURL()is setup to return an array of elements, but you are referencing the return value as a single element, try this: