Entering server, username and password within this piece of code

Hi,

I am using this code but I do not understand how to connect to the database. Where should I enter the server, username, password, etc.? Should it be only in the bit of code: $this->host, $this->username, $this->password:

<?php

class modernCMS {
	
	var $host;
	var $username;
	var $password;
	var $db;
	
	function connect() {
		$con = mysql($this->host, $this->username, $this->password) or die(mysql_error());
		mysql_select_db($this->db, $con) or die(mysql_error());
	}
	
	function add_content($p) {
		$name = mysql_real_escape_string($p['name']);
		$price = mysql_real_escape_string($p['price']);
	}
}
?>

That code is going to use the host, username, password and db properties of the class to connect to the DB. The code doesn’t actually specifiy the value of those properties which means they will all be blank, and the db connection will fail.

Set the values directly or in a function:


    var $host = 'localhost';

    var $username = 'myuser';

    var $password = 'mypass';

    var $db = 'mydbname';


Then you need to instantiate the class and call the connect method to invoke the MySQL connection:


$myDb = new modernCMS();
$myDb->connect();

I thought this bit of code connected to the database:

function connect() { 
        $con = mysql($this->host, $this->username, $this->password) or die(mysql_error()); 
        mysql_select_db($this->db, $con) or die(mysql_error()); 
    } 

but i do not understand the $this->host bit. Does it mean the same as just $host? If so, why bother with the $this bit?

The connect() function does indeed do the MySQL connection, however it gets the username/pass etc from the class properties.

When you use var $host in your class like that, it becomes a property. You can only access properties by using $this-> syntax, simply $host will return an undefined variable notice.

By the way your class is PHP4, I recommend looking at PHP5 classes.

OK

So why do you say I need

$myDb = new modernCMS(); 
$myDb->connect();

??

Why do I need to connect that way when I have function to connect already?

And what does “new modernCMS” mean? Why use the word ‘new’??

I would recommend reading something like this: Object-Oriented PHP for Beginners | Nettuts+

If you are going to achieve what you want to, you need to understand how the language works, and in this case, object oriented programming too.

The code in your first post is a class which if executed on the server, will not do anything until you have some code that will make use of the class. The code I posted instantiates the class (initialises) then calls the connect function. Without that, it will do nothing.

The use of classes and objects is known as Object Oriented Programming (OOP), have a look at some online guides, there are loads found on Google.

OK - Yes, I need to read up about this.

Can you just explain one other thing for me in relation to this.

<?php
if($_POST['add']):
$obj->add_content($_POST);
endif;
?>

Someone did say this is PHP4. I thought when using if functions they were written like this:


if(this){
do this}

So why is the code I am using using a “:” instead of a “{”?
And what does endif mean?

Is this PHP4 and if so should I use the { instead to start the if function and } to end it?

Or is the coding for if functions different when using objects?

By the way the first two lines of

if($_POST[‘add’]):
$obj->add_content($_POST);

are producing an error!!

I would be grateful if you could at least explain how if statements work with objects, if not point out why I am getting an error.

Thanks,

Matt.

The code is using a : instead of { purely out of preference of whoever wrote it.

In PHP you can use that syntax for an if, or the { version like:


if($_POST['add']):
     // do something
endif;

// is the same as

if($_POST['add'])
{
    // do something
}

You’re getting an error because you haven’t defined $obj, you can’t just come up with a variable name and assume it will be your class.

You’d need to do:


$obj = new modernCMS(); // this defines $obj as an object of your class.
$obj->add_content($_POST); // now we have defined $obj, you can call any methods of it

I mentioned your class is PHP4, this is denoted by the use of var $host; syntax. PHP5 uses a different syntax for declaring properties, a PHP5 propertly can look like:


class modernCMS
{
    private $host = 'localhost';
}

You’ll see the differences when looking at PHP5 classes example code.