Jump to content

[SOLVED] Display multiple db rows


forumnz

Recommended Posts

How does that work?

 

Sya I have 10 users and want to display them all, how can I code something that places all 10 (say username and location) in rows as output. I know how to do the html part etc, and also how can I make it so that every odd number row has light grey and every even has darker?

 

Thanks a lot,

Sam.

Link to comment
Share on other sites

Something like....

 

<?php

 // connect to db.

 $sql = "SELECT uname,location FROM users";
 if ($result = mysql_query($sql)) {
   if (mysql_num_rows($result)) {
     $i = 1;
     echo "<table>";
     while ($row = mysql_fetch_assoc($result)) {
       $color = ($i % 2) ? 'light' : 'dark';
       echo "  <tr class='$color'>";
       echo "    <td>{$row['uname']}</td>";
       echo "    <td>{$row['location']}</td>";
       echo "  </tr>";
       $i++;
     }
     echo "</table>";
   } else {
     echo "No results found";
   }
 } else {
   echo "Query failed " . mysql_error() . "<br />$sql";
 }

?>

Link to comment
Share on other sites

Why does this not work (thanks thorpe)? It displays all of it on the same line.

 

<?php


//Connect to mysql server
$link=mysql_connect("localhost","sss","sss");
if(!$link) {
	die('Failed to connect to server: ' . mysql_error());
}
//Select database
$db=mysql_select_db("bbmembers");
if(!$db) {
	die("Unable to select database");

}
else{

  $sql = "SELECT * FROM auctions";
  if ($result = mysql_query($sql)) {
    if (mysql_num_rows($result)) {
      $i = 1;
      echo "<table>";
      while ($row = mysql_fetch_assoc($result)) {
        $color = ($i % 2) ? 'light' : 'dark';
        echo "  <td class='$color'>";
        echo "    <tr>{$row['uan']}</tr>";
        echo "    <tr>{$row['title']}</tr>";
        echo "  </td>";
        $i++;
      }
      echo "</table>";
    } else {
      echo "No results found";
    }
  } else {
    echo "Query failed " . mysql_error() . "<br />$sql";
  }
  }

?>

Link to comment
Share on other sites

while ($row = mysql_fetch_assoc($result)) {
        $color = ($i % 2) ? 'light' : 'dark';
        echo "  <tr class='$color'>";
        echo "    <td>{$row['uname']}</td>";
        echo "    <td>{$row['location']}</td>";
        echo "  </tr>";
        $i++;

Link to comment
Share on other sites

Oh I see... haha.... One other thing, how can I change the order ...ie asc or desc? I wrote ORDER BY asc in the select part but i got an error:

Query failed 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 LIMIT 3' at line 1
SELECT * FROM auctions ORDER BY desc LIMIT 3

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.