New To PHP - Error In My Class

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • cyber0ne

    New To PHP - Error In My Class

    Using PHP 5.1.2

    I'm working on a personal project to get used to PHP, and I seem to be
    stuck at the moment on a class I'm writing. Here is the code for the
    class, with a comment highlighting where it dies when a function is
    called externally from the code that instantiates the class:

    ----------BEGIN PASTE----------
    class DataAccess{

    private $databaseConnec tion;

    function __construct()
    {
    @ $databaseConnec tion = new mysqli("this", "is", "fake", "data");
    if (mysqli_connect _errno())
    {
    // TODO: generate an error
    }
    }

    function __destruct()
    {
    $databaseConnec tion->close();
    }

    function GetArticlesForC ategory($cid)
    {
    $querySelect = "SELECT stuff WHERE something = " . $cid;

    // Testing shows that the following line is where the code dies.
    $resultSet = $databaseConnec tion->query($querySe lect);
    // End dying code

    if ($resultSet)
    {
    $rowCount = $resultSet->num_rows;
    if ($rowCount 0)
    {
    $i = 0;
    while ($i < $rowCount)
    {
    $rows[$i] = $resultSet->fetch_assoc( );
    $i++;
    }
    $resultSet->free();
    return $rows;
    }
    else
    {
    // TODO: report an empty set
    $resultSet->free();
    return;
    }
    }
    else
    {
    // TODO: generate an error
    $resultSet->free();
    return;
    }
    }

    }
    ------------END PASTE------------

    The error log seems to be claiming that $databaseConnec tion is
    undefined on that line of code. Is there maybe something syntactic
    that I'm missing? I've tried replacing all 3 references to
    $databaseConnec tion in the class functions with
    $this->$databaseConne ction, but that didn't seem to change anything.

    Any ideas?

  • Tim Hunt

    #2
    Re: New To PHP - Error In My Class


    cyber0ne wrote:
    Using PHP 5.1.2
    >
    I'm working on a personal project to get used to PHP, and I seem to be
    stuck at the moment on a class I'm writing. Here is the code for the
    class, with a comment highlighting where it dies when a function is
    called externally from the code that instantiates the class:
    >
    ----------BEGIN PASTE----------
    class DataAccess{
    >
    private $databaseConnec tion;
    >
    function __construct()
    {
    @ $databaseConnec tion = new mysqli("this", "is", "fake", "data");
    if (mysqli_connect _errno())
    {
    // TODO: generate an error
    }
    }
    >
    function __destruct()
    {
    $databaseConnec tion->close();
    }
    >
    function GetArticlesForC ategory($cid)
    {
    $querySelect = "SELECT stuff WHERE something = " . $cid;
    >
    // Testing shows that the following line is where the code dies.
    $resultSet = $databaseConnec tion->query($querySe lect);
    // End dying code
    >
    if ($resultSet)
    {
    $rowCount = $resultSet->num_rows;
    if ($rowCount 0)
    {
    $i = 0;
    while ($i < $rowCount)
    {
    $rows[$i] = $resultSet->fetch_assoc( );
    $i++;
    }
    $resultSet->free();
    return $rows;
    }
    else
    {
    // TODO: report an empty set
    $resultSet->free();
    return;
    }
    }
    else
    {
    // TODO: generate an error
    $resultSet->free();
    return;
    }
    }
    >
    }
    ------------END PASTE------------
    >
    The error log seems to be claiming that $databaseConnec tion is
    undefined on that line of code. Is there maybe something syntactic
    that I'm missing? I've tried replacing all 3 references to
    $databaseConnec tion in the class functions with
    $this->$databaseConne ction, but that didn't seem to change anything.
    >
    Any ideas?
    You have to use $this->... to access class properties

    And...
    @ $databaseConnec tion = new mysqli("this", "is", "fake", "data");
    if (mysqli_connect _errno())
    {
    // TODO: generate an error
    }
    }
    How can you tell if an error occurs if the mysqli error message's are
    being displayed and php errors are suppressed for 'new mysqli'?

    Replace every $databaseConnec tion with $this->databaseConnec tion,
    replace TODO with echo mysql_connect_e rror() and remove the @ from
    $databaseConnec tion.

    Tim

    Comment

    • B.r.K.o.N.j.A

      #3
      Re: New To PHP - Error In My Class

      cyber0ne wrote:
      Using PHP 5.1.2
      >
      I'm working on a personal project to get used to PHP, and I seem to be
      stuck at the moment on a class I'm writing. Here is the code for the
      class, with a comment highlighting where it dies when a function is
      called externally from the code that instantiates the class:
      >
      ----------BEGIN PASTE----------
      class DataAccess{
      >
      private $databaseConnec tion;
      >
      function __construct()
      {
      @ $databaseConnec tion = new mysqli("this", "is", "fake", "data");
      if (mysqli_connect _errno())
      {
      // TODO: generate an error
      }
      }
      >
      function __destruct()
      {
      $databaseConnec tion->close();
      }
      >
      function GetArticlesForC ategory($cid)
      {
      $querySelect = "SELECT stuff WHERE something = " . $cid;
      >
      // Testing shows that the following line is where the code dies.
      $resultSet = $databaseConnec tion->query($querySe lect);
      // End dying code
      >
      if ($resultSet)
      {
      $rowCount = $resultSet->num_rows;
      if ($rowCount 0)
      {
      $i = 0;
      while ($i < $rowCount)
      {
      $rows[$i] = $resultSet->fetch_assoc( );
      $i++;
      }
      $resultSet->free();
      return $rows;
      }
      else
      {
      // TODO: report an empty set
      $resultSet->free();
      return;
      }
      }
      else
      {
      // TODO: generate an error
      $resultSet->free();
      return;
      }
      }
      >
      }
      ------------END PASTE------------
      >
      The error log seems to be claiming that $databaseConnec tion is
      undefined on that line of code. Is there maybe something syntactic
      that I'm missing? I've tried replacing all 3 references to
      $databaseConnec tion in the class functions with
      $this->$databaseConne ction, but that didn't seem to change anything.
      >
      Any ideas?
      >
      the way you did it in constructor the variable databaseConnect ion has
      scope only in that particular method, you should use

      @ $this->databaseConnec tion = new mysqli("this", "is", "fake", "data");

      and allways reference that property via $this->databaseConnec ion...

      you were using local variables where you really needed class properties


      --

      B.r.K.o.N.j.A = Bionic Robotic Knight Optimized for Nocturnal Judo and
      Assasination

      Comment

      • cyber0ne

        #4
        Re: New To PHP - Error In My Class

        Replace every $databaseConnec tion with $this->databaseConnec tion

        I guess it was a syntax thing :) I was trying the following:

        $databaseConnec tion
        or
        $this->$databaseConne ction

        But I just got rid of that second $ where applicable and it seems to be
        working now. Thanks!

        Comment

        Working...