Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Mar 2009
    Posts
    97
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Using $this when not in object contex

    Getting this error

    Fatal error: Using $this when not in object context in C:\wamp\www\register_oop\index.php on line 7
    here is my code

    PHP Code:
    <?php
    error_reporting
    (E_ALL);

    require_once 
    "database.php";

    $sql "SELECT * FROM `users` WHERE `id` = 1";
    $res $this->query($sql);
    $row $this->fetch_array($res);

    echo 
    $row['username'];

    ?>

  • #2
    New Coder
    Join Date
    Mar 2009
    Location
    Chicago, IL
    Posts
    69
    Thanks
    0
    Thanked 15 Times in 15 Posts
    You need to create new database object first. Don't know what your database class is called, but assuming it's called "database":

    PHP Code:
    <?php
    error_reporting
    (E_ALL);

    require_once 
    "database.php";

    $db = new database();

    $sql "SELECT * FROM `users` WHERE `id` = 1";
    $res $db->query($sql);
    $row $db->fetch_array($res);

    echo 
    $row['username'];
    ?>

  • #3
    Senior Coder timgolding's Avatar
    Join Date
    Aug 2006
    Location
    Southampton
    Posts
    1,517
    Thanks
    114
    Thanked 110 Times in 109 Posts
    The class name is most probably in that database.php file. Look for the line with
    Code:
     class
    On it and it will be the word after that
    You can not say you know how to do something, until you can teach it to someone else.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •