faqts : Computers : Programming : Languages : PHP

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

5 of 7 people (71%) answered Yes
Recently 5 of 7 people (71%) answered Yes

Entry

I can only get the last record from a database to be displayed, how do I get all records to display?

Feb 24th, 2008 00:08
dman, Fabian Mellegård, Andy Wright, http://www.ttnr.org


Here is how I do it. There are probarbly other way but ...
/Fabian
I have a table created like this:
CREATE TABLE doc_tab (doc_id INT NOT NULL   DEFAULT '0' AUTO_INCREMENT,
                      docnr       VARCHAR(9)   NOT NULL,
                      PRIMARY KEY (doc_id),
                      UNIQUE doc_id (doc_id),
                      UNIQUE docnr (docnr));
To get all entrys displayed I simply write:
<?php
  // Opens the database
  $datab = mysql_connect("localhost");
  mysql_select_db("doctest",$datab);
  // query to get all the document numbers. Get the numbers in reversed 
  // order, i.e. last stored record first.
  $query = "SELECT doc_id, docnr FROM doc_tab ORDER BY doc_id DESC";
  $result = mysql_query($query, $datab) or die ("Could not execute
query: $query");
  // Print all the records
  while ($doc_data = mysql_fetch_row($result)) 
  {
    echo "document number: $doc_data[1] <br>";
  }
?>