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
https://forums.phpfreaks.com/topic/70408-solved-display-multiple-db-rows/
Share on other sites

actually uou need

 

$link= mysql connect or die mysql error

$result = mysql_query("SELECT * FROM table", $link);

 

 

$num_rows = mysql_num_rows($result);

 

echo "<font face=Verdana size=1 color=484077> $num_rows </font>";

 

 

 

 

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";
 }

?>

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";
  }
  }

?>

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++;

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.