If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > PHP > PHP and MySQL

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-28-10, 12:25
david112 david112 is offline
Registered User
 
Join Date: Apr 2010
Posts: 1
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!
Reply With Quote
  #2 (permalink)  
Old 06-01-10, 18:09
ProphetX ProphetX is offline
Registered User
 
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
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On