Getting Info From DB

Discussion in 'PHP' started by NaSh123, Jun 26, 2008.

  1. #1
    Can someone tell me what is wrong with this?

    
    <?php 
    include("databaseinfo.php");
    $query = mysql_query("SELECT intro FROM layoutdesc WHERE cat='$idd'"); 
    $description = $row['intro'];
    echo $description;
    ?>
    
    Code (markup):
    I basically wrote an introduction to each page of my site and i want to access a database that I have made that has 3 values, NR which is just an increment to how many rows I have, category which is the page title and intro which is the introduction.

    layoutdesc= table name
    Fields= nr, intro, cat

    For some reason no text displays and I have no idea why.
     
    NaSh123, Jun 26, 2008 IP
  2. wowla_123

    wowla_123 Peon

    Messages:
    147
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Modify the code as follows:

    <?php 
    include("databaseinfo.php");
    $query = mysql_query("SELECT intro FROM layoutdesc WHERE cat='$idd'");
    while($row = mysql_fetch_array($query, MYSQL_BOTH))
       {
       $description = $row['intro'];
       echo $description;
       }
    ?>
    Code (markup):
     
    wowla_123, Jun 26, 2008 IP
  3. NaSh123

    NaSh123 Peon

    Messages:
    1,298
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Could you possibly explain why we need the while loop just so I can learn for the future? Also is there an easier way to access the field from a table or is this the only way to get it done?
     
    NaSh123, Jun 26, 2008 IP
  4. wowla_123

    wowla_123 Peon

    Messages:
    147
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #4
    While loop is needed if you are expecting more than one row. The while-loop will keep iterating through the records and display the data. If there is only one row (or zero row), you can skip the while loop and replace it with the following code:

    <?php 
    include("databaseinfo.php");
    $query = mysql_query("SELECT intro FROM layoutdesc WHERE cat='$idd'");
    $row = mysql_fetch_array($query, MYSQL_BOTH);
    $description = $row['intro'];
    echo $description;
    ?>
    Code (markup):
    I think there is no easier way to get the data than this.
     
    wowla_123, Jun 26, 2008 IP
  5. NaSh123

    NaSh123 Peon

    Messages:
    1,298
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I got it, so basically if the database ONLY had one row then the command would work but if I have 50 rows for example the while look will cycle through the database and pull out all the match's for the category correct? Or do you mean that if we are expecting more then one match? I have about 27 rows in the database and only 1 will match per query so should I use the second method or the first?
     
    NaSh123, Jun 26, 2008 IP
  6. wowla_123

    wowla_123 Peon

    Messages:
    147
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Exactly! If you have 50 rows and you don't use while loop, only first row will be fetched.
     
    wowla_123, Jun 26, 2008 IP
  7. NaSh123

    NaSh123 Peon

    Messages:
    1,298
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Great! Thanks for your help.
     
    NaSh123, Jun 26, 2008 IP