PDA

View Full Version : Adding queries the "right way"


zonegray
11-17-2002, 01:31 PM
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:<?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:<?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
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: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