Web Analytics Made Easy -
StatCounter displaying single rows of data - CodingForum

Announcement

Collapse
No announcement yet.

displaying single rows of data

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • displaying single rows of data

    can anyone give me some pointers as to how to create a display page? I have a large DB of bands and wish to generate each page one row at a time. I know how to connect to the db etc, just how to display each individual data. Would the URL be www.mysite.com/bands.php?bandname="bandname"...?


    THaanks
    Dave

  • #2
    Probably.

    You need to pass an unique value to the detail-display page, so you can identify the row of your DB table which data should be retrieved and displayed. The bandname could be such a value, but generally it's quite possible that two bands share the same name. Better it is to assign a numerical primary key to each row, call this field "ID", and pass that to the display page.

    Alltogether the URL would look like

    bands.php?id=321

    On the display page, you grab the id value from the $_GET array, validate it that it really contains only a numerical value (intval() helps here), and use that value in your query. The query might look like

    Code:
    $query = "SELECT
    	name,
    	genre,
    	band_picture_url
    FROM
    	bands
    WHERE
    	id = " . stripslashes($id);
    Does that help? If not, try to give more details on what was unclear.
    De gustibus non est disputandum.

    Comment


    • #3
      above mordred's code, add the following:

      PHP Code:
      $id $_GET["id"]; 


      some complete code:
      PHP Code:
      <?php

      /* Database Connection Info */
      $db_name      "db_bands";
      $sql_port     "3306";
      $sql_server   "www.mysite.com";
      $sql_user     "sql_user";
      $sql_password =  "password";
      $band_table   "tbl_bands";


      $id $_GET["id"];


      /* Establish the connection to the database */
      $connection   mysql_connect($sql_server ":" $sql_port$sql_user$sql_password);
      $db           mysql_select_db($db_name) or die ("Unable to select database.");

      /* Query the database, return the fields, and print information */
      $query  "SELECT * FROM '$band_table' WHERE band_id = '$id'";
      $result mysql_query($query,$db) or die ('Queryproblem: ' mysql_error());
      if (
      $result) {
          
      $row=mysql_fetch_assoc($result);
          print 
      "Artist Name: " $row["band_name"] . "<br/>";
          print 
      "Artist Genre: " $row["band_genre"] . "<br/>";

      } else {
         print 
      "Sorry...that band does not appear to be in our database.";
      }
      ?>
      Now obviously, I don't know your database connection information, nor do I know what information you're storing in the database (like genre, album titles, etc), nor do I know what those fields are called, but the above should give you a fairly good idea of what you can use.

      HTH,
      -Celt
      Last edited by Celtboy; Feb 8, 2004, 12:13 PM.

      Comment


      • #4
        Thanks to both of you! Thats real helpful!
        Dave

        Comment

        Working...
        X