0

I am trying to take all the records in my database and display them row by row in a table. It only displays the same row again and again. How can I make each row of the table display the next result in the database?

<?php
// Formulate query
$logo = "SELECT logo from stores";
$cat = "SELECT cat from stores";
$commission = "SELECT commission from stores";
$link = "SELECT link from stores";
$name = "SELECT name from stores";
// Perform query
$result1 = mysql_query($logo) or die;
$result2 = mysql_query($cat) or die;
$result3 = mysql_query($commission) or die;
$result4 = mysql_query($link) or die;
$result5 = mysql_query($name) or die('Something went wrong');
//////////////////////////////////////
//////////////////////////////////////
do {
//////////////////////////////////////
$rlogo = mysql_fetch_assoc($result1);
$a = implode($rlogo);
//////////////////////////////////////
$rcat = mysql_fetch_assoc($result2);
$b = implode($rcat);
//////////////////////////////////////
$rcommission = mysql_fetch_assoc($result3);
$c = implode($rcommission);
//////////////////////////////////////
$rlink = mysql_fetch_assoc($result4);
$d = implode($rlink);
//////////////////////////////////////
$rname = mysql_fetch_assoc($result5);
$e = implode($rname);
//////////////////////////////////////
$x = $x + 1;
    } while ($x <= 1);
?>
Tim
  • 2,123
  • 4
  • 27
  • 44
  • Please don't use `mysql_*` functions anymore, they are deprecated. See [Why shouldn't I use mysql_* functions in PHP?](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) for details. Instead you should learn about [prepared statements](http://bobby-tables.com/php.html) and use either [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli). If you can't decide which, [this article](http://php.net/manual/en/mysqlinfo.api.choosing.php) will help you. If you pick PDO, [here is a good tutorial](http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers). – Marcel Korpel Nov 10 '13 at 21:23
  • 1
    You have to loop through the fetches result. `while ($rlogo = mysql_fetch_assoc($result1)) { ... }` – Royal Bg Nov 10 '13 at 21:24
  • Why are you running five queries on the table when one will return all the data? –  Nov 10 '13 at 21:27
  • There is another column that I don't want to get so instead of selecting *, I just selected the specific columns I wanted – Tim Nov 10 '13 at 21:33
  • @773 look at my answer and tell me if you have some questions, please – gloomy.penguin Nov 10 '13 at 21:33

1 Answers1

1

if you're incrementing $x but the loop ends if it reaches 1... it ends right away?

   $x = $x + 1;
} while ($x <= 1);

normally, people set it up like this:

 $query = "select logo, cat, commission, link, name  from stores";

 $result = mysql_query($query); 

 print "<table>";

 // table headers 
 print "<tr><th>logo</th>
            <th>cat</th>
            <th>comission</th>
            <th>link</th>
            <th>name</th></tr>";

 while($row = mysql_fetch_assoc($result))
 {
     print "<tr>"; 

     foreach ($row as $column => $value) 
     {
         print "<td>".$value."</td>";
     }

      // or you can print the table cells like this: 
      //  <td> $row['logo']      </td>
      //  <td> $row['cat']       </td>
      //  <td> $row['commission']</td>
      //  <td> $row['link']      </td>
      //  <td> $row['name']      </td>

     print "</tr>"; 
 }

 print "</table>;

also, mysql_ functions are out-dated and will be removed from PHP soon so if you're learning, you should learn PDO or mysqli_ instead.

gloomy.penguin
  • 5,833
  • 6
  • 33
  • 59