Page 1 of 2 12 LastLast
Results 1 to 10 of 16

Thread: display mysql entries on site?

  1. #1
    Join Date
    Feb 2010
    Location
    Falkirk, Scotland
    Posts
    142
    Thanks
    21
    Thanked 4 Times in 4 Posts

    Post display mysql entries on site?

    i finally got my head round inserting data in mysql through an html form, but now i want to display all of the table entries on a page on my site.

    can anyone let me know how to do it?
    any help will be appreciated, thanx
    Last edited by liamallan; 02-16-2010 at 03:12 AM.

  2. #2
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Something like:
    Code:
    $query = mysql_query("SELECT * FROM `table`") or die(mysql_error());
    while($row = mysql_fetch_assoc($query)){
      echo $row['field'];
    }
    Jeremy | jfein.net

  3. #3
    Join Date
    Feb 2010
    Location
    Falkirk, Scotland
    Posts
    142
    Thanks
    21
    Thanked 4 Times in 4 Posts

    Default

    yes, something like that mate. but i want to add entries in a table.
    the fields on my db are: name and profile

  4. #4
    Join Date
    Aug 2009
    Posts
    399
    Thanks
    42
    Thanked 4 Times in 4 Posts

    Default

    $name = 'thomas';
    $profile = 'profile';

    $query = mysql_query("INSERT INTO table(name, profile) VALUES('$name', '$profile')") or die(mysql_error());

  5. #5
    Join Date
    Feb 2010
    Location
    Falkirk, Scotland
    Posts
    142
    Thanks
    21
    Thanked 4 Times in 4 Posts

    Default

    im new to php, so you might need to explain a little.

    my situation is: created form on shiftysplayground.co.cc/addfriends.php to post data (name, profile) into database.
    the plan is to have a table of users below the form on shiftysplayground.co.cc/addfriends.php, taken from database.
    will i need to add the php code to the page addfriends.php or the file supplying the action?

  6. #6
    Join Date
    Nov 2006
    Location
    Northeast USA
    Posts
    408
    Thanks
    8
    Thanked 30 Times in 28 Posts

    Default

    assuming the form on addfriends.php has an action of process and a method of POST, you would combine the code\ideas from the above posts:
    PHP Code:
    <?php
    //process.php
    $host="yourhost";
    $username="yourusername";
    $password="yourpass";
    $db_name="yourDB";
    $tbl_name="yourTABLE";

    mysql_connect("$host""$username""$password")or die("cannot connect to server");
    mysql_select_db("$db_name")or die("cannot select db");

    $name $_POST['name'];
    $profile $_POST['profile'];

    $query mysql_query("INSERT INTO $tbl_name(name, profile) VALUES('$name', '$profile')") or die(mysql_error()); 
    header('location: addfriends.php');
    ?>
    and your add friends page that dispalys a list:
    PHP Code:
    <?php
    //process.php
    $host="yourhost";
    $username="yourusername";
    $password="yourpass";
    $db_name="yourDB";
    $tbl_name="yourTABLE";

    mysql_connect("$host""$username""$password")or die("cannot connect to server");
    mysql_select_db("$db_name")or die("cannot select db");
    $query mysql_query("SELECT * FROM `table`") or die(mysql_error());
    ?>
    <!-- Here's where your form goes, then place this beneath it or wherever you want the list.... -->
    <table width="400" border="0" cellspacing="1" cellpadding="0">
    <tr>
    <td>
    <table width="400" border="1" cellspacing="0" cellpadding="3">
    <tr>
    <td colspan="2"><strong>Entries</strong> </td>
    </tr>

    <tr>
    <td align="center"><strong>Name</strong></td>
    <td align="center"><strong>Profile</strong></td>

    </tr>
    <?php
    while($rows=mysql_fetch_array($query)){
    ?>
    <tr>
    <td><? echo $rows['name']; ?></td>
    <td><? echo $rows['profile']; ?></td>
    </tr>
    <?php
    }
    ?>
    </table>
    </td>
    </tr>
    </table>
    ****Untested******
    -Ben -- THE DYNAMIC DRIVERS
    My Links: My DD Profile||My Youtube Video Tutorials||DD Helping Coders||DD Coders In Training
    I told my client to press F5, the client pressed F, then 5, *facepalm*

  7. The Following User Says Thank You to fileserverdirect For This Useful Post:

    liamallan (02-16-2010)

  8. #7
    Join Date
    Feb 2010
    Location
    Falkirk, Scotland
    Posts
    142
    Thanks
    21
    Thanked 4 Times in 4 Posts

    Default

    thank you mate, it seems to be inputting the data into the table very well.

    i have another question though,

    new entries seem to be joining at the bottom of the table, how could i make it so they join at the top?

    also, the data entered into the profile field is a url, is there a way i could make the url fields a clickable link that reads 'add friend'?

    thanks again for your help

  9. #8
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    To make it display descending change:
    Code:
    $query = mysql_query("SELECT * FROM `table`") or die(mysql_error());
    To:
    Code:
    $query = mysql_query("SELECT * FROM `table` DESC") or die(mysql_error());
    For profile, change:
    Code:
    <td><? echo $rows['profile']; ?></td>
    To:
    Code:
    <td><a href="<? echo $rows['profile']; ?>">Add Friend</a></td>
    Jeremy | jfein.net

  10. #9
    Join Date
    Feb 2010
    Location
    Falkirk, Scotland
    Posts
    142
    Thanks
    21
    Thanked 4 Times in 4 Posts

    Default

    You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DESC' at line 1
    does that mean it doesnt recognise 'desc'?

  11. #10
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    No, it needs to order iteself by something... Do you have an id or date column?
    Jeremy | jfein.net

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
  •