PDA

View Full Version : Efficient PHP for other web pages?


Shaman
Mon 26th Jun '00, 8:09pm
<?php
$mysql_link = mysql_connect("xx.server.net","public","shoutitout");

$query= "select count(userid) from user";
$mysql_result = mysql_db_query("kosforum", $query, $mysql_link);
$numusers = mysql_fetch_array($mysql_result);

$query="select count(threadid) from thread";
$mysql_result = mysql_db_query("kosforum", $query, $mysql_link);
$numthreads = mysql_fetch_array($mysql_result);

$query="select count(postid) from post";
$mysql_result = mysql_db_query("kosforum", $query, $mysql_link);
$numposts = mysql_fetch_array($mysql_result);

$query="select count(sessionid) from session";
$mysql_result = mysql_db_query("kosforum", $query, $mysql_link);
$numonline = mysql_fetch_array($mysql_result);

mysql_close($mysql_link);

print ("<font color=#FFFFFF>Netforum</font> members: ");
echo $numusers['0'];
print (", threads: ");
echo $numthreads['0'];
print (", posts: ");
echo $numposts['0'];
print (", now online: ");
echo $numonline['0'];
?>

This returns:

"Netforum members: 64, threads: 87, posts: 219, now online: 4"

for my tiny little board. ;)

The question is, can't I make that considerably more efficient yet? /me is a casual programmer and has only done some PHP in the past little while.

Bealers
Thu 29th Jun '00, 4:37am
Well you could create a new table and add the four values as fields, then hunt around in the code for where the addition/deletion bits for each related feilds are and increment/decrease the values accordingly

then it would simply be a matter of "select * from stats_table"

not sure if it's worth it though, the queries are not exactly taxing

also if there are any relationships between the tables a join would save on queries, I don't know about vBulletin though

BTW I would make the following changes to your posted code:
though yours was just as syntactically correct

<?php
mysql_connect("xx.server.net","public","shoutitout");
@mysql_select_db("kosforum") or die "mysql is dead";

$query= "select count(userid) as numusers from user";
$mysql_result = mysql_query($query);
# mysql link is inferred
$numusers = mysql_result($mysql_result,0,'numusers');

$query="select count(threadid) as threadnum from thread";
$mysql_result = mysql_query($query);
$numthreads = mysql_result($mysql_result,0,'threadnum');

$query="select count(postid) as numposts from post";
$mysql_result = mysql_query($query);
$numposts = mysql_result($mysql_result,0,'numposts');

$query="select count(sessionid) loggedin from session";
$mysql_result = mysql_query($query);
$numonline = mysql_result($mysql_result,0,'loggedin');

mysql_close();

print ("<font color=#FFFFFF>Netforum</font> members: ");
echo $numusers;
print (", threads: ");
echo $numthreads;
print (", posts: ");
echo $numposts;
print (", now online: ");
echo $numonline;
?>

mysql_fetch_array is really cool, but you don't need to use it all the time and I'm sure on big data sets that it would use more memory
<imo/>