Show text from database on HTML or PHP page.

Discussion in 'PHP' started by marktopper, Sep 13, 2011.

  1. #1
    How can I show some text from the database on my PHP or HTML pages.

    I have the database table called "pages" and inside there I have "homepage".

    I need to make it possible to get the text inside the database out at the page?
    Any commands for this :D

    I am using MySQL.
     
    Solved! View solution.
    marktopper, Sep 13, 2011 IP
  2. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #2
    In a PHP file, use SQL to select the field(s) you want from the database. Use the echo command to send it to the user. (Yes, you have to write code.)
     
    Rukbat, Sep 13, 2011 IP
  3. gvre

    gvre Member

    Messages:
    35
    Likes Received:
    6
    Best Answers:
    3
    Trophy Points:
    33
    #3
    Search php manual for mysql_query. You will find many examples there.
     
    gvre, Sep 13, 2011 IP
  4. #4
    An example tweaked from the php.net site below. It should get you started...

    <?php
    mysql_connect("localhost", "mysql_user", "mysql_password") or
        die("Could not connect: " . mysql_error());
    mysql_select_db("mydb");
    
    $result = mysql_query("SELECT homepage FROM pages");
    
    while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
    {
        echo $row["homepage"];
    }
    
    mysql_free_result($result);
    ?>
    
    PHP:
     
    mfscripts, Sep 13, 2011 IP
  5. marktopper

    marktopper Member

    Messages:
    30
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #5
    Thanks a lot :D I got it working.
     
    marktopper, Sep 14, 2011 IP
  6. fortehlolz

    fortehlolz Banned

    Messages:
    111
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I know this is solved, but surely you want to encourage against mysql? Considering it's going to become deprecated because of it's vulnerabilities?

    I suggest PDO or MySQLi.

    <?php
    $Host = 'localhost';
    $User = 'your username';
    $Pass = 'Your pass';
    
    try {
        $con = new PDO("mysql:host=$Host;dbname=mysql", $User, $Pass);
          echo 'Database connection made';
        }
    catch(PDOException $e)
        {
        echo $e->getMessage();
        }
        
    $Query = $con->prepare('SELECT row FROM table');
    $Query->execute();
      while($Row = $Query->fetch(PDO::FETCH_ASSOC)) {
        echo htmlentities($Row['row'], ENT_QUOTES, "UTF-8");  // htmlentities on output assuming it has not been cleaned
      }
    
    PHP:
     
    fortehlolz, Sep 14, 2011 IP
  7. mfscripts

    mfscripts Banned

    Messages:
    319
    Likes Received:
    4
    Best Answers:
    8
    Trophy Points:
    90
    Digital Goods:
    3
    #7
    What vulnerabilities? Where does it say it's going to become deprecated?
     
    mfscripts, Sep 14, 2011 IP
  8. fortehlolz

    fortehlolz Banned

    Messages:
    111
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #8
    mysql has a tendancy to be easily exploited.

    news.php.net/php.internals/53799

    Obviously it's long term yet, but there's the news. It's advised that you moved over to MySQLi or PDO. PDO protects against SQL injections and is faster, not to mention it's use of database abstraction and it's a lot nicer to look at, IMO.
     
    fortehlolz, Sep 14, 2011 IP
  9. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #9
    Since marktopper isn't using user input for his select parameters, SQL injection would be moot.
     
    Rukbat, Sep 14, 2011 IP
  10. marktopper

    marktopper Member

    Messages:
    30
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #10
    Ohh. Thanks I will check it out.
    Thanks for telling me it is vulnerabilities
     
    marktopper, Sep 14, 2011 IP
  11. mfscripts

    mfscripts Banned

    Messages:
    319
    Likes Received:
    4
    Best Answers:
    8
    Trophy Points:
    90
    Digital Goods:
    3
    #11
    Thanks for the link, I had no idea. Agreed it's definately worth keeping in mind for development and probably better to look at other options such as PDO as you mentioned. It would help if php.net also placed this information on the mysql_* documentation page too!
     
    mfscripts, Sep 15, 2011 IP
  12. fortehlolz

    fortehlolz Banned

    Messages:
    111
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #12
    Yes, I know. Regarding the SQL injections, I was simply answering why PDO is better, because of the way it seperates data/syntax and binding params.

    Obviously you can't rely solely on PDO as a "OMG my site is secure now" method, you still need to escape other data, XSS, CSRF and so on. And yes, it'd be much easier if PHP made it more clear of their plans
     
    fortehlolz, Sep 15, 2011 IP