Display mySQL query with PHP on a webpage

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bigitup
    New Member
    • Oct 2012
    • 2

    Display mySQL query with PHP on a webpage

    Hi,

    I have created a database on a website using phpmyadmin and I would like to display the result of a mySQL query on a webpage using PHP.

    I used the following code and nothing appears on my webpage:

    Code:
    <?php
    // mySQL database connection
    
    $con = mysql_connect("db_server", "username", "password");
    if (!$con) 
    {
    die ('Could not connect: ' . mysql_error());
    }
    
    mysql_select_db("db_name", $con);
    
    $query = 'SELECT * FROM person';
    
    $result = mysql_query($query);
    
    while($row = mysql_fetch_assoc($result));
    {
    echo $row['name'];
    echo $row['surname'];
    echo $row['country'];
    }
    ?>
    Can anyone help me fix this, please ?
  • Luuk
    Recognized Expert Top Contributor
    • Mar 2012
    • 1043

    #2
    Change "db_server" to "localhost" on line 4?

    and change "db_name" to that databasename you created in phpmyadmin (line 10)

    Comment

    • bigitup
      New Member
      • Oct 2012
      • 2

      #3
      Well, I put db_name and username in the post because the host of the database is not localhost on the server where my site is hosted and for privacy . I found the solution though. I have to upgrade my account, my database is not remotely accessible

      Comment

      • Luuk
        Recognized Expert Top Contributor
        • Mar 2012
        • 1043

        #4
        hmm, is it not 'local' to where your PHP script is running?

        If the script is running at 'example.com', and your database is also at 'example.com', than you should put 'localhost' in the servername...

        Comment

        Working...