Log in

View Full Version : accessing query results on custom page


Oreamnos
04-27-2005, 06:11 AM
i am attempting to build my first vb powered page via this tutorial: https://vborg.vbsupport.ru/showthread.php?t=62164

everything from that tut is working fine but i am running a query and cant seem to access the results in a template.

basically this is what i have:
<?php
// ####################### SET PHP ENVIRONMENT ###########################
error_reporting(E_ALL & ~E_NOTICE);

// #################### DEFINE IMPORTANT CONSTANTS #######################
define('NO_REGISTER_GLOBALS', 1);
define('THIS_SCRIPT', 'contest_stats'); // change this depending on your filename

// ################### PRE-CACHE TEMPLATES AND DATA ######################
// get special phrase groups
$phrasegroups = array();

// get special data templates from the datastore
$specialtemplates = array();

// pre-cache templates used by all actions
$globaltemplates = array(
'contest_stats',
);

// pre-cache templates used by specific actions
$actiontemplates = array();

// ######################### REQUIRE BACK-END ############################
require_once('./global.php');

// ################################################## #####################
// ######################## START MAIN SCRIPT ############################
// ################################################## #####################
$startdate = 1112313600;
$enddate = 1114819200;
$get_thread_stats = "<a href=\"contest_stats.php?do=stats&type=threads\">Get Thread Starter Stats</a>";

if ($_REQUEST['do'] == "stats") {
// user has requested stats so get them
// chould we get most threads started, most posts or most referrals?
if ($_REQUEST['type'] == "threads") {

$most_threads = "";
$threads = $DB_site->query_first("
SELECT postusername
FROM " . TABLE_PREFIX . "thread");

while ($row = $DB_site->fetch_array($threads)) {
$most_threads .= $row['postusername'] . "<br />";
}


} elseif ($_REQUEST['type'] == "posts") {

} elseif ($_REQUEST['type'] == "referrals") {

}


}



$navbits = array();
$navbits[$parent] = 'Contest Statistics';
$navbits = construct_navbits($navbits);
eval('$navbar = "' . fetch_template('navbar') . '";');
eval('print_output("' . fetch_template('contest_stats') . '");');

?>

template
$stylevar[htmldoctype]
<html dir="$stylevar[textdirection]" lang="$stylevar[languagecode]">
<head>
<title>$vboptions[bbtitle]</title>
$headinclude
</head>
<body>
$header

$navbar

<table class="tborder" cellpadding="$stylevar[cellpadding]" cellspacing="$stylevar[cellspacing]" border="0" width="100%" align="center">
<tr>
<td class="tcat">Title</td>
</tr>
<tr>
<td class="alt1">

$get_thread_stats
<br />
$most_threads
<br />
$row[postusername]

</td>
</tr>
</table>

$footer
</body>
</html>

i have $most_threads and $row[postusername] but still no luck.

any ideas how i can access my query results?

thanks
eric

Marco van Herwaarden
04-27-2005, 06:49 AM
If you are using a query_first (as opposed to simple "query") you don't need to fetch the rows afterwards, this is automatically done.

So just remove the while block and replace with:
$most_threads = $threads['postusername'];

PS query_first will only retrieve 1 row.

Oreamnos
04-27-2005, 08:18 AM
ok, thanks for heads up on the query_first thing. i thought that was a little weird.

i still couldnt get it to work without a while loop.

heres what worked for me:$threads = $DB_site->query("SELECT * FROM " . TABLE_PREFIX . "thread");

while ($thread = $DB_site->fetch_array($threads)) {
$most_threads .= $thread['postusername'] . "<br />";
}

thanks again

eric

Marco van Herwaarden
04-27-2005, 09:42 AM
Yes if you use "query" then you will need a loop to read the rows.

Oreamnos
04-27-2005, 09:43 AM
cool, thanks for your help. im rolling now :D