0

I am making a PHP blog library. I am new to web development and only really programme in C++ or Python. So, I have probably done something really stupid, but this code doesn't work:

<?php
class Blog
{
    public $version = 1.0;
    public $mysql = NULL;

    public $servername = NULL;
    public $username = NULL;
    public $password = NULL;
    public $dbname = NULL;

    public function connectToDatabase($server, $user, $pass, $db) {
        $this->$servername = $server;
        $this->$username = $user;
        $this->$password = $pass;
        $this->$dbname = $db;
        $this->$mysql = mysql_connect($this->$servername, $this->$username, $this->$password);
        if (!$this->$mysql) {
            die('Error 500: Internal server error. :(<br /><br />Could not connect to ' . $this->$servername . ': ' . mysql_error());
            return false;
        }

I get the error: Fatal error: Cannot access empty property in /home/tom/public_html/Projects/PHPBlog/blog.php on line 13

3
Contributors
4
Replies
6
Views
7 Years
Discussion Span
Last Post by TomMan
-1

I am making a PHP blog library. I am new to web development and only really programme in C++ or Python. So, I have probably done something really stupid, but this code doesn't work:

<?php
class Blog
{
    public $version = 1.0;
    public $mysql = NULL;

    public $servername = NULL;
    public $username = NULL;
    public $password = NULL;
    public $dbname = NULL;

    public function connectToDatabase($server, $user, $pass, $db) {
        $this->$servername = $server;
        $this->$username = $user;
        $this->$password = $pass;
        $this->$dbname = $db;
        $this->$mysql = mysql_connect($this->$servername, $this->$username, $this->$password);
        if (!$this->$mysql) {
            die('Error 500: Internal server error. :(<br /><br />Could not connect to ' . $this->$servername . ': ' . mysql_error());
            return false;
        }

I get the error: Fatal error: Cannot access empty property in /home/tom/public_html/Projects/PHPBlog/blog.php on line 13

try to define your server name as localhost

0

When referencing vars, you do not need a second $ :

$this->$servername = $server;

Would become

$this->servername = $server;

@tmash
The whole idea of Object Orientated programming is to make your code portable, not every server will have MySQL on localhost so defining this absolutely is not a good idea. The server address is passed to the script when the function is called.

Edited by Will Gresham: n/a

0

When referencing vars, you do not need a second $ :

$this->$servername = $server;

Would become

$this->servername = $server;

@tmash
The whole idea of Object Orientated programming is to make your code portable, not every server will have MySQL on localhost so defining this absolutely is not a good idea. The server address is passed to the script when the function is called.

yes you are right i did not perfectly see that error:
$this->servername = $server;

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.