PDA

View Full Version : help with query


aussiev8
05-16-2006, 09:53 AM
can anyone help?

$sql = $DB_site->query("SELECT COUNT( * ) AS texts FROM encyclopedia_text WHERE title LIKE '$ltr%'");

i've got the error,
Fatal error: Call to a member function on a non-object in /home/aussie7/public_html/encyclopedia.php on line 58

The Geek
05-16-2006, 10:06 AM
$DB_site was for 3.0x series use $db for 3.5.x

Another reason is that you may need to put:
global $DB_site;
or
global $db;

above the code to make it accessible if it is in a function.

HTH's

aussiev8
05-16-2006, 10:17 AM
thanks, but neither worked, even with the globals.

Hellcat
05-16-2006, 10:25 AM
vB 3.0 or 3.5?

For 3.5 you could also try this:
global $vbulletin;
$r = $vbulletin->db->query_write( "...." );

aussiev8
05-16-2006, 10:31 AM
nah it didn't work either.
and version 3.5

below is a snippet of code, its going to be an encyclopedia that i'm going to share here. It was working on 3.0 and i'm rewriting it for 3.5 (now i have some spare time) and all the other scripts in my vb dir have $DB_site.

// #################### DEFINE IMPORTANT CONSTANTS #######################
define('NO_REGISTER_GLOBALS', 1);

define('THIS_SCRIPT', 'encyclopedia');



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

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

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

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

// ######################### REQUIRE BACK-END ############################
require_once('./global.php');
require_once('./includes/functions_bbcodeparse.php');
require_once('./includes/functions_user.php');
require_once('./includes/adminfunctions.php');

// ################################################## #####################
// ######################## START MAIN SCRIPT ############################
// ################################################## #####################


if (empty($_REQUEST['do']))
{
$_REQUEST['do'] = 'list';
}

global $vbulletin;
$r = $vbulletin->db->query_write( "select * from encyclopedia");

$alpha = array ("A","B","C","D","E","F","G","H","I","J","K","L","M",
"N","O","P","Q","R","S","T","U","V","W","X","Y","Z");
$num = count($alpha);

for($x = 0; $x < $num; $x++) {
$ltr = $alpha[$x];
$sql = $vbulletin->$db->query("SELECT COUNT( * ) AS texts FROM encyclopedia_text WHERE title LIKE '$ltr%'");
$numrows = $vbulletin->$db->fetch_Array($sql);

The Geek
05-16-2006, 11:04 AM
change:

global $vbulletin;
$r = $vbulletin->db->query_write( "select * from encyclopedia");


to


$r = $db->query_read("SELECT * FROM " . TABLE_PREFIX . "encyclopedia");


Futhermore, your query for the letters is incorrect AND will have A LOT of overhead (at least 26 additional gob searches).

aussiev8
05-16-2006, 11:35 AM
$db->query_read did the trick... thanks..

i can't log into the API, it keeps throwing errors, then takes ages to load... bit of a pitty. but thanks to everyone who helped

The Geek
05-16-2006, 12:00 PM
no problemo.

I would think about something like a table with the following fields:
id, symbol, total

then when someone edits or creates a new entry, take the first letter of the entry and update the above table accordingly.
Then you can just dosomething like:

$results = $db->query_read("SELECT symbol, total FROM " . TABLE_PREFIX . "encyclopedia_dir ORDER BY Symbol ASC");

$my_nav = array();

while ($result = $db->fetach_array($results))
{
$my_nav[$result['symbol']] = $result['total'];
}


Thats just one of 1,000's ways to do what you're after without the MASSIVE overhead of doing an aggregate 26 times on a table via a gob search.

The other way is to then cache the above results in the datastore or in a file. Then you can avoid the query every page refresh (however, a query like that would be VERY minor).

Good luck.

hambil
05-16-2006, 12:35 PM
nah it didn't work either.
and version 3.5

below is a snippet of code, its going to be an encyclopedia that i'm going to share here. It was working on 3.0 and i'm rewriting it for 3.5 (now i have some spare time) and all the other scripts in my vb dir have $DB_site.

// #################### DEFINE IMPORTANT CONSTANTS #######################
define('NO_REGISTER_GLOBALS', 1);

define('THIS_SCRIPT', 'encyclopedia');



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

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

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

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

// ######################### REQUIRE BACK-END ############################
require_once('./global.php');
require_once('./includes/functions_bbcodeparse.php');
require_once('./includes/functions_user.php');
require_once('./includes/adminfunctions.php');

// ################################################## #####################
// ######################## START MAIN SCRIPT ############################
// ################################################## #####################


if (empty($_REQUEST['do']))
{
$_REQUEST['do'] = 'list';
}

global $vbulletin;
$r = $vbulletin->db->query_write( "select * from encyclopedia");

$alpha = array ("A","B","C","D","E","F","G","H","I","J","K","L","M",
"N","O","P","Q","R","S","T","U","V","W","X","Y","Z");
$num = count($alpha);

for($x = 0; $x < $num; $x++) {
$ltr = $alpha[$x];
$sql = $vbulletin->$db->query("SELECT COUNT( * ) AS texts FROM encyclopedia_text WHERE title LIKE '$ltr%'");
$numrows = $vbulletin->$db->fetch_Array($sql);

Also, you don't want the second $:
wrong: $vbulletin->$db
right: $vbulletin->db