0

I'm currently doing a populating/editing my DB using PHP on a webpage I'm going to build. I'm relatively new to PHP so please excuse my "greenhorn" knowledge. My DB connection works, however, I don't understand why my function "displaySQL" and then counting the quantity as an array is not generating anything on my webpage. There's no number displayed on the screen, I was going to have that value put inside an HTML table afterwards. My DB settings are also established for the parameter part (host, username, password). Thanks for any help.

class User{

var $conn;
function __construct($hostname,$username,$password){
    try{
        $conn = new PDO("mysql:host=$hostname;dbname=pjj5",
        $username, $password);
        echo "Connected successfully <br>";
    }
    catch(PDOException $e)
    {
        echo "Connection failed: " . $e->getMessage();
    }

}

//display all users information
function displaySQL(){

    $sql = "SELECT * FROM accounts";

    $q = $conn->prepare($sql);
    $q->execute();
    $results = $q->fetchAll();
    return $results;
}

$db = new User($hostname,$username,$password);
$res = $db->displaySQL();
echo count($res);
tadman
  • 208,517
  • 23
  • 234
  • 262
y17
  • 11
  • 7
  • 1
    : "It looks like you're writing your own ORM. Have you considered using one that's already written, tested, and widely supported like [Doctrine](http://www.doctrine-project.org/), [Propel](http://propelorm.org/) or [Eloquent](https://laravel.com/docs/master/eloquent)?" – tadman Nov 07 '17 at 02:53
  • I'll check it out, but for now I just want to start at a basic scale of understanding things myself. – y17 Nov 07 '17 at 02:55
  • 2
    @tadman Isn't writing an ORM an obvious beginner project? – Barmar Nov 07 '17 at 02:55
  • What version of PHP is this meant to be? It looks like PHP4. Turn error reporting on at the top of your script by adding the following `error_reporting(E_ALL)`. Also you're not assigning `$conn` to an instance variable, so it won't be accessible in your `displaySql` function. There are other issues too. – fubar Nov 07 '17 at 02:56
  • `$conn` should be `$this->conn`. PHP is not C++, class properties cannot be accessed as normal variables. – Barmar Nov 07 '17 at 02:57
  • @Barmar I'd agree *if* the person attempting this has already used a few so they know how they work in principle *and* in practice. There's a lot to understand before embarking on this journey, it's really not for the person who's still learning PHP fundamentals. An obvious beginner project is learning how Eloquent works, and once they've got a good grasp on that, try and make their own as an academic exercise. – tadman Nov 07 '17 at 02:57
  • @tadman Sorry, I forgot to put a smiley, it was a joke. – Barmar Nov 07 '17 at 02:57
  • It may be advisable to look at the source code of existing ORMs to understand the programming concepts in PHP. – fubar Nov 07 '17 at 02:58
  • @Barmar Around here I can never tell. The PHP community has a worrying allergy to frameworks that's quite disturbing. At times I wonder why some don't write their own OS to avoid using someone else's code. – tadman Nov 07 '17 at 02:58
  • 1
    Barmar, put a smiley? Now *this* I have to see :-)) – Funk Forty Niner Nov 07 '17 at 03:02
  • @tadman it is helpful to try to build your own ORM to understand why you shouldn't do so. – serakfalcon Nov 07 '17 at 03:41
  • @serakfalcon That's a long, painful process that's best contained in an academic environment, not when you're actually trying to build a site. – tadman Nov 07 '17 at 03:42
  • @tadman I guess it depends if you're getting paid for it or just doing it as a hobby. I was assuming the latter but totally agree with you if it is a paid project. Although I'd hope that a newbie to php would be doing such things as a hobby :) – serakfalcon Nov 07 '17 at 03:43

1 Answers1

0

To access a variable of an object within the object you need to use the syntax $this->.

This line:

    $conn = new PDO("mysql:host=$hostname;dbname=pjj5",
        $username, $password);

and this line

$q = $conn->prepare($sql);

should have $this->conn instead of $conn. In the first function you create the connection to a new $conn variable that is local in scope to the function only, which gets discarded after the object is constructed. For the function displaySQL() $conn is undefined.

serakfalcon
  • 3,501
  • 1
  • 22
  • 33