#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    3
    Rep Power
    0

    PHP to write html


    Hello board, I have a section on my website that shows a list of dates and whats on. They also link out to separate web pages.

    Currently I have to go in and amend this each week but what I would like to do is to input this information into my database (MySQL) and then the PHP code takes this out and updates the page with the next 6 events by date order.

    Is this possible and if so do you have any examples i could use to start with?

    Any help would be appreciated with this as I am fairly new to programming.
  2. #2
  3. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,317
    Rep Power
    7170
    Yes it's possible, and a fairly straight forward piece of code to implement.

    PHP Code:
    <?php

        $username 
    "dbusername";
        
    $password "dbpassword";
        
    $host "localhost";
        
    $dbname "dbname";

        
    $db = new PDO("mysql:host={$host};dbname={$dbname};charset=utf8"$username$password);
        
    $db->setAttribute(PDO::ATTR_ERRMODEPDO::ERRMODE_EXCEPTION);
        
    $db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODEPDO::FETCH_ASSOC);

        
    $sql "SELECT `column1`,`column2` FROM `table` ORDER BY `column2` DESC LIMIT 6;"
        
    $st $db->prepare($sql);
        
    $st->execute();
        
    $rows $st->fetchAll();
        foreach(
    $rows as $row)
        {
            echo 
    $row['column1'] . '<br />';
        }
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    3
    Rep Power
    0
    Originally Posted by E-Oreo
    Yes it's possible, and a fairly straight forward piece of code to implement.

    PHP Code:
    <?php

        $username 
    "dbusername";
        
    $password "dbpassword";
        
    $host "localhost";
        
    $dbname "dbname";

        
    $db = new PDO("mysql:host={$host};dbname={$dbname};charset=utf8"$username$password);
        
    $db->setAttribute(PDO::ATTR_ERRMODEPDO::ERRMODE_EXCEPTION);
        
    $db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODEPDO::FETCH_ASSOC);

        
    $sql "SELECT `column1`,`column2` FROM `table` ORDER BY `column2` DESC LIMIT 6;"
        
    $st $db->prepare($sql);
        
    $st->execute();
        
    $rows $st->fetchAll();
        foreach(
    $rows as $row)
        {
            echo 
    $row['column1'] . '<br />';
        }
    Thanks for your help E-Oreo

    I have tried this out but the PHP code is showing at the top of the page and is not showing the data from the database. I will play around with this and let you know how it goes.
  6. #4
  7. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,850
    Rep Power
    6351
    Your server isn't set up to parse PHP files properly.

    Also, PHP's entire purpose is to generate HTML content. That's literally all it's for.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    12
    Rep Power
    0
    Please check, if your httpd.conf (Apache configuration) has the following line:

    AddType application/x-httpd-php .php

    If you are on virtual hosting - send letter to tech staff or try to write that line in .htaccess file

IMN logo majestic logo threadwatch logo seochat tools logo