Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6840129
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:48:13+00:00 2026-05-26T23:48:13+00:00

I am learning php and made a simple application to save peoples names to

  • 0

I am learning php and made a simple application to save peoples names to a database then pull all the saved names into a list lower down on the page.

this is the code that is getting the names: echo "<div class=\"results\"> $row[Name] </div>";

I put a div with the class of results so that I could style it. At this point all I wanted to do was center the results. After styling it with css to be centered, looking at the page source each name that was echo’ed has a div around it with the class of results. They are all centered like I wanted which is good but this does not seem like the best way to do it because there will be a new div created for every new name in the database (right now there are 18 divs. Way too many!).I was hoping only one div would be created but I was obviously wrong about that.

So, is it possible to pre-create a div in the html markup with nothing in and echo the names into the div? Or do you think there would be a way to make it all output at once so it was all within the one div?

Here is the rest of the code so you get an idea of whats happening:

<?php 
$connect = mysql_connect("localhost","user","0123");
if(!$connect){
    die("Failed to connect: " . mysql_error());
}

if(!mysql_select_db("Test")) {
    die("Failed to select database: " . mysql_error());
}

$results = mysql_query("SELECT * FROM Friends");

while($row = mysql_fetch_array($results)){
    echo "<div class=\"results\"> $row[Name] </div>";
}

if ($_POST[name] !="") { $sql="INSERT INTO Friends 
(name) VALUES('$_POST[name]')";

if (!mysql_query($sql,$connect))
  {
  die('Error: ' . mysql_error());
  }
echo "<div id=\"added\"> $_POST[name] added to the database </div>";
}
else die("")

?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T23:48:13+00:00Added an answer on May 26, 2026 at 11:48 pm

    Since you output the div inside your while loop, you will get a new div for each result. Just move the div outside the loop:

    echo "<div class=\"results\">";
    while($row = mysql_fetch_array($results)){
        echo $row['Name'];
    }
    echo "</div>";
    

    Note that you should always quote your keys. Use $row['Name'] instead of $row[Name] (obviously not if Name is a defined constant).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently learning PHP. I've made a simple script @ http://hash.techho.me , only thing
I'm learning to put values into my db from php. this is my simple
I made a very simple Facebook application in PHP that retrives information about the
I'm learning Eclipse PDT for PHP development. In pretty much all tutorials I've read
Learning php! I have a sql database which contains a table called 'images' which
So far all the sites I have made have been in PHP, I'm thinking
Just learning php and looking into someone else's code. I'm not sure what is
im learning php and to keep it simple (i think), this is what im
I am crash-learning PHP for a project, and I have a ton of stupid
I am learning PHP PEAR and testing the following code. However when I login,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.