Log in

View Full Version : simple while statement/query not working


Nullifi3d
02-09-2006, 10:53 PM
$links = $db->query_first("SELECT pl_href AS href, pl_title AS title, pl_anchor AS anchor FROM " . TABLE_PREFIX . "user WHERE pl_href != ''");
while ($link = $db->fetch_array($links)) eval('$premium_links = "' . fetch_template('directory_premium_links') . '";');

I've added the three fields you see above to the user table so I can print out a list of websites on a certain page. However, when I use query_first it doesn't print anythign out. When I use query_read it only prints out the last link that would show (because read is not an array function). Why doesn't it work with first?

tnguy3n
02-09-2006, 10:56 PM
TRY:
$links = $db->query("SELECT pl_href AS href, pl_title AS title, pl_anchor AS anchor FROM " . TABLE_PREFIX . "user WHERE pl_href != ''");
while ($link = $db->fetch_array($links))
{
eval('$premium_links .= "' . fetch_template('directory_premium_links') . '";');
}

Marco van Herwaarden
02-10-2006, 07:47 AM
When I use query_read it only prints out the last link that would show (because read is not an array function).To print all when using query_read(), change the eval statement to:
eval('$premium_links .= "' . fetch_template('directory_premium_links')
Why doesn't it work with first?
Because you don't need the while-statement or the fetch_array() if you're using query_first. query_first will already restrieve the row (query_first never return more then 1 row). With query_first you would need just (asuming your query would never return more then 1 row:
$link = $db->query_first("SELECT pl_href AS href, pl_title AS title, pl_anchor AS anchor FROM " . TABLE_PREFIX . "user WHERE pl_href != ''");
eval('$premium_links = "' . fetch_template('directory_premium_links') . '";');

$links = $db->query'query' is depreciated in vB3.5, use query_read or query_write instead.

Nullifi3d
02-10-2006, 01:05 PM
thanks. I got it to work by adding the decimal before the =