0

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.

Roger Williams
  • 159
  • 2
  • 7
  • 15

1 Answers1

0

The method getLyricsFromURL() is setup to return an array of elements, but you are referencing the return value as a single element, try this:

public function getLyricsFromURL($lyricsid){

    // This isn't necessary
    //$results;

     // Better to use $stmt as the variable here, obviously this is subjective
     // but will serve you better when you are working on other stmt objects
     $stmt = $this->con->prepare("SELECT * FROM `lyrics` WHERE lyrics_id = :lyricsid");

     $stmt->bindParam(':lyricsid', $lyricsid,PDO::PARAM_INT);
     $stmt->execute();

     $results = $stmt->fetchAll(PDO::FETCH_OBJ);

     return $results;
}

...

// Instatiate our lyrics object
$lyric = new lyrics($conn);

// Make request for lyrics associated to row #55 and store results in local var
$lyrics = $lyric->getLyricsFromURL(55);

// Notice we are referencing the first element of the array
<h1><?php echo $lyrics[0]->lyrics_title;?></h1>
Mike Purcell
  • 19,847
  • 10
  • 52
  • 89
  • Thanks for your response but this didn't work :(. Received this error: `Fatal error: Cannot use object of type lyrics as array` not sure if this helps but i chose to echo out the results as objects: `$results = $stmt->fetchAll(PDO::FETCH_OBJ)` – Roger Williams Jul 24 '12 at 23:51
  • Understood, but the fetchall should be returning an array of anonymous objects based on the schema (column names), which should be accessible via standard array calls. Can you do a `var_dump($lyrics);` after the `$lyrics = $lyric->getLyricsFromURL(55);` line and post the results on http://pastebin.com/? – Mike Purcell Jul 25 '12 at 00:00
  • Ya, that's not what I expected, did you implement my code changes as outlined in answer? I don't see how you are getting an object construct like the one you posted. – Mike Purcell Jul 25 '12 at 00:09
  • yes I did, I'm not sure whats going on! :( it's been frustrating my for days. – Roger Williams Jul 25 '12 at 00:17
  • It's a subtle change and may have been overlooked, but did you notice that the lyric object was changed from `lyrics` to `lyric`? – Mike Purcell Jul 25 '12 at 00:19
  • Can you post the entire contents of the `lyrics` class on pastebin? – Mike Purcell Jul 25 '12 at 00:31
  • Looking at the code I don't see how it is generating that unexpected object, can you do a `var_dump($this->con)` in the `getLyricsFromURL` method? Post the results in pastebin. – Mike Purcell Jul 25 '12 at 16:34
  • Sorry for the late response here is the link: http://pastebin.com/BMQfZvvL.........if it makes a difference I am using dependency injection design pattern to connect all the other classes that depends on a database connection "connection class". – Roger Williams Jul 25 '12 at 19:59
  • First off you don't need to prefix a `var_dump` with an `echo`. Also, the `var_dump($results)` is exactly what I was expecting. Notice it is an array containing an anonymous (standard) object. So if this the the value of `$results` in the `getLyricsFromURL` method, then the method is working fine, the problem is with your calling code, can you post the calling code via pastebin? – Mike Purcell Jul 25 '12 at 20:24
  • 1
    If the calling code is as you have it in the pastebin then the problem is you did not update it using my suggested changes. I'd recommend making the suggested changes and see if it works. – Mike Purcell Jul 25 '12 at 21:12
  • wow it worked this time.maybe the problem was that the last time I used the code I just copies apart of it. Thanks Man. – Roger Williams Jul 25 '12 at 21:22
  • 1
    No problem, glad to hear you got it working. By the way good job on getting into PDO early, it will help you down the road. – Mike Purcell Jul 25 '12 at 21:30