Image

Imagejulisana wrote in Imagephp

More OOP Woes!

Thank you so much, Imagenickinuse!!! You solved all my issues this time around!

Hi Guys!

Imagejustindustin was so helpful with my last problem and I've made progress with my project, but I'm running into another issue.

Basically, none of my data members can keep their values!

I have two files: index.php and social_media.php. index.php is going to be the data display file, while social_media.php is the class file. index.php has a require_once('social_media.php'), a new Social_Media object, and a call to one of my getter functions in Social_Media.

The object creates just fine. I have some echo statements in my __constructor that display all the data members after they've had values set to them, so I *know* they're set.

Unfortunately, when I go to call my getter function, which includes an echo statement letting me know whether or not the variable has a value, I get a "no value" response.

The getter function is also supposed to pull the value from the database if the value doesn't exists for some reason. But instead of getting the value, it gets me a "Fatal error: Call to a member function prepare() on a non-object" error!

Seriously, what am I doing wrong?


class Social_Media
{
  protected $_db;
  protected $network_name;               //Name of social media network.  I.E. Facebook, twitter, etc.
  protected $client_id;                  //WHMCS generated ID for client.
  protected $username;                   //Username for $network_name
  protected $password;                   //Password for $network_name
  protected $profile_url;                //URL for $network_name's profile
  protected $widgets;

  function Social_Media($c_id, $network)
  {
    $client_id = $c_id;
    $network_name = $network;
    $query = "SELECT * FROM social_media ";
    $query .= "WHERE client_id = :client_id AND network_name = :network_name ";
    $query .= "LIMIT 0, 1;";
    $params = array(":client_id" => $client_id, ":network_name" => $network_name);
    
    try
    {
      $_db = new PDO(connection_data);     //PDO object - Database connection
      try
      {
        $result = $_db->prepare($query);
        $result->execute($params);
        
        $row = $result->fetch(PDO::FETCH_ASSOC);
        if (isset($row))
        {
          $username = $row['username'];
          $password = $row['password'];
          $profile_url = $row['profile_url'];
          //Testing Purposes Only.
          echo "Client ID: " . $client_id . "<br />Network Name: " . $network_name . "<br />Username: " . $username . "<br />Password: " . $password . "<br />Profile URL: " . $profile_url . "<br />";
        } //end if
      } //end try
      catch (PDOException $e)
      {
        echo $e->getMessage();
      } //end catch
    } //end try
    catch (PDOException $ee)
    {
      echo $ee->getMessage();
    } //end catch
  } //end __constructor

  public function getUsername()
  {
    if (isset($username))
    {
      echo "<br />Username has a value!";
      return $username;
    } //end if
    else
    {
      echo "<br />Username doesn't have a value!";
      $query = "SELECT username FROM social_media ";
      $query .= "WHERE client_id = :client_id AND network_name = :network_name ";
      $query .= "LIMIT 0, 1;";
      $params = array(":client_id" => $client_id, ":network_name" => $network_name);
      
      try
      {
        $result = $_db->prepare($query);
        $result->execute($params);
        
        foreach($result->fetch() as $row)
        {
          $username = $row['username'];
          break;
        }
        return $username;
      }
      catch (PDOException $e)
      {
        echo $e->getMessage();
      } //end catch
    } //end else
  } //end getUsername()
} //end Social_Media


Here are the results that display to my screen right now.

Client ID: 1
Network Name: facebook
Username: my_username
Password: my_password
Profile URL: http://www.facebook.com/my_account

Username doesn't have a value
Fatal error: Call to a member function prepare() on a non-object in /home/julisana/social_media/social_media.php on line 140

getUsername starts on line 123, so line 140 is inside that function.