Sign in to follow this  
future_man

Display database rows in html by id

Recommended Posts

Hey, I want to list all of my database entries to a nice html format. For every unique id there is one row displayed out from my database to my php/html. I just don't know how to do that. I tried using while loop and foreach I just don't know how to properly structure them... My query and php code looks like this:

$query = mysql_query("SELECT * FROM sport");

while($row = mysql_fetch_array($query))
{
    $result = $row['sport_ime'].' '.$row['sezona'];
}

Share this post


Link to post
Share on other sites

Say that your table has 3 columns and you want to list all entries separated by a ',' and break a line after each entry:

$query = "SELECT * FROM sport";
$result = mysql_query($query);
$foundResults = mysql_fetch_array($result);
if(mysql_error())
{
    echo 'Error in query [' . mysql_error() . ']<br>';
}
else if ($foundResults == NULL)
{
    echo 'No results.<br>';
}
else
{
    do
    {
       echo $foundResults[0] . ', ' . $foundResults[1] . ', ' . $foundResults[2] . '<br>';
    } 
    while(($foundResults = mysql_fetch_array($result, MYSQL_NUM)) != NULL);
}

Hope that helps.

Edited by CJ_COIMBRA

Share this post


Link to post
Share on other sites

may i ask how your using mysql in visual studio?

 

The OP is using PHP and thus most likely isn't using Visual Studio.

 

The way you'd use MySQL in ASP.Net is quite different from the way you'd use it in C++, even though you can use Visual Studio in both cases so you should probably be a bit more specific.

Share this post


Link to post
Share on other sites

Say that your table has 3 columns and you want to list all entries separated by a ',' and break a line after each entry:

$query = "SELECT * FROM sport";
$result = mysql_query($query);
$foundResults = mysql_fetch_array($result);
if(mysql_error())
{
    echo 'Error in query [' . mysql_error() . ']<br>';
}
else if ($foundResults == NULL)
{
    echo 'No results.<br>';
}
else
{
    do
    {
       echo $foundResults[0] . ', ' . $foundResults[1] . ', ' . $foundResults[2] . '<br>';
    } 
    while(($foundResults = mysql_fetch_array($result, MYSQL_NUM)) != NULL);
}

Hope that helps.

 

Thank you, I did a similar approach! Turns out you really need to just put a break tag at the end and echo it :D how simple is that

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this