SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Evangelist
    Join Date
    Mar 2011
    Posts
    423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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 Code:
    <?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']);
        }
    }
    ?>

  2. #2
    SitePoint Enthusiast
    Join Date
    Sep 2009
    Posts
    53
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by MatthewBOnline View Post
    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 Code:
    <?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:

    PHP Code:
        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:

    PHP Code:
    $myDb = new modernCMS();
    $myDb->connect(); 

  3. #3
    SitePoint Evangelist
    Join Date
    Mar 2011
    Posts
    423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I thought this bit of code connected to the database:

    PHP Code:
    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?

  4. #4
    SitePoint Enthusiast
    Join Date
    Sep 2009
    Posts
    53
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by MatthewBOnline View Post
    I thought this bit of code connected to the database:

    PHP Code:
    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.

  5. #5
    SitePoint Evangelist
    Join Date
    Mar 2011
    Posts
    423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK

    So why do you say I need

    PHP Code:
    $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'??

  6. #6
    SitePoint Guru r2d2's Avatar
    Join Date
    Dec 2003
    Location
    In my van, fool!
    Posts
    646
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  7. #7
    SitePoint Enthusiast
    Join Date
    Sep 2009
    Posts
    53
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by MatthewBOnline View Post
    OK

    So why do you say I need

    PHP Code:
    $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'??
    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.

  8. #8
    SitePoint Evangelist
    Join Date
    Mar 2011
    Posts
    423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK - Yes, I need to read up about this.

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

    PHP Code:
    <?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:

    PHP Code:
    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.

  9. #9
    SitePoint Enthusiast
    Join Date
    Sep 2009
    Posts
    53
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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:

    PHP Code:
    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:

    PHP Code:
    $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:

    PHP Code:
    class modernCMS
    {
        private 
    $host 'localhost';

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


Bookmarks

Posting Permissions

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