Results 1 to 2 of 2

Thread: PHP and MySQL

  1. #1
    Join Date
    Apr 2010
    Posts
    1

    Unanswered: PHP and MySQL

    Hey there guys,

    I've got a bit of a problem with some SQL, and I'm hoping that you'll be able to help me out. I've got the following code:

    function getTerms($db) {
    $sql = "SELECT termname FROM tblterms";
    $stmt = $db->prepare($sql);
    $stmt = $db->query($sql);
    $terms = $stmt->fetch();
    return $terms;
    }

    In the past I've used this code, but I've always had a "LIMIT 1" tacked onto the end of the SQL statement. What I am hoping for here is a list of entries from the termname column of the table, all of which I already know are guaranteed to be unique, so I don't have to worry about duplicates there. I know that PHP returns this in an array, but I'm not entirely sure how to reference each row, or indeed count how many rows there are! Any / all help would be much appreciated.

    Thanks very much!

  2. #2
    Join Date
    Jun 2010
    Location
    New Zealand
    Posts
    15
    PHP Code:
    function getTerms($db)
    {
        
    $sql "SELECT termname FROM tblterms";
        
    $stmt $db->prepare($sql);
        
    $result $db->query($stmt);
        return 
    $result;
    }

    $terms getTerms($db);

    while(
    $row mysql_fetch_array($terms))
    {
        
    $termname $row['termname'];
        echo 
    $termname.'<br />';

    The while() loop will go through each record that your query returns. Hope that helps

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •