vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   Member Archives (https://vborg.vbsupport.ru/forumdisplay.php?f=202)
-   -   Adding queries the "right way" (https://vborg.vbsupport.ru/showthread.php?t=45820)

zonegray 11-17-2002 01:31 PM

Adding queries the "right way"
 
I'm building a new front page (/index.php), and I need to understand the "right way" to add queries.

I include global.php, and have access to all my templates, etc. and it works fine. Now I want to add a query. Since I know just enough PHP to be dangerous, I first tried it using Dreamweaver MX. And it works, but I'm obviously opening an extra database connection, including files that I shouldn't etc. Here's what Dreamweaver created:
Code:

<?php require_once('Connections/msfdev.php'); ?>
<?php
mysql_select_db($database_msfdev, $msfdev);
$query_Recordset1 = "SELECT MAX(lastpost) FROM forum WHERE newsforum = 1 ";
$Recordset1 = mysql_query($query_Recordset1, $msfdev) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);

$lastupdate=$row_Recordset1['MAX(lastpost)'];

The included file "msfdev" simply includes the connection information:
Code:

<?php
# FileName="Connection_php_mysql.htm"
# Type="MYSQL"
# HTTP="true"
$hostname_msfdev = "localhost";
$database_msfdev = "dbname";
$username_msfdev = "dbuser";
$password_msfdev = "password";
$msfdev = mysql_pconnect($hostname_msfdev, $username_msfdev, $password_msfdev) or die(mysql_error());
?>

How can I rewrite this more efficiently, using the config info from vBulletin and/or any functions available via global.php?

BTW, the particular query I'm doing selects the timestamp of the newest thread in my news forums, which are denoted by a field that I added to the db (newsforum="1"). As I said, it works fine as is, but I know that it's sloppy programming.

Xenon 11-17-2002 02:42 PM

try this:

PHP Code:

<?php
require('global.php');

mysql_select_db($database_msfdev$msfdev);
$query_Recordset1 "SELECT MAX(lastpost) FROM forum WHERE newsforum = 1 ";
$Recordset1 $DB_site->query($query_Recordset1);
$row_Recordset1 $DB_site->fetch_array($Recordset1);
$totalRows_Recordset1 $DB_site->num_rows($Recordset1);
$lastupdate=$row_Recordset1['MAX(lastpost)'];

that should be what you wanted.

zonegray 11-17-2002 04:47 PM

Thanks... that's exactly what I'm after, but it only "almost" works.

Problem is with the line:
Code:

mysql_select_db($database_msfdev, $msfdev);
Those variables are defined in the Dreamweaver connection file. Are there some variables defined in global.php that I can substitute in the mysql_select_db statement?

zonegray 11-17-2002 05:05 PM

Duh... RTFM, read the function description, deleted the line, and now it works.

Thanks. I've been looking for how to do this, couldn't find many refs with a search. I tried to figure out the $DB_site object, but wasn't having any luck. From the looks of it, I can just call it from anywhere, and it'll derive the connection info from vB.

Xenon 11-18-2002 04:11 PM

You're welcome ;)

the DB_site objects can be found in db_mysql.php in your admin directory


All times are GMT. The time now is 12:32 AM.

Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.

X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.01089 seconds
  • Memory Usage 1,722KB
  • Queries Executed 10 (?)
More Information
Template Usage:
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (3)bbcode_code_printable
  • (1)bbcode_php_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (5)printthreadbit
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • showthread
Included Files:
  • ./printthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/class_bbcode_alt.php
  • ./includes/class_bbcode.php
  • ./includes/functions_bigthree.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • printthread_start
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete