Php code help

Soldato
Joined
29 Dec 2004
Posts
5,653
Location
Chatham, Kent
Simple i'm sure, the following code -

Code:
            <?php $handle = mysql_connect('localhost', 'root', '');

$datastoretemp = mysql_query(
 "SELECT title, data
  FROM forums
  WHERE title = 'userstats'",
  $handle);

while ($storeitem = mysql_fetch_array($datastore))
{
  $datastore["$storeitem[title]"] = $storeitem['data'];
}

// get total members and newest member from template
$userstats = unserialize($datastore['userstats']);
$numbermembers = number_format($userstats['numbermembers']);
$newusername = $userstats['newusername'];
$newuserid = $userstats['newuserid'];




//Extract a list of forums and corresponding stats
$handle = mysql_connect('localhost', 'root', '');
$forumtemp = mysql_query("SELECT * FROM forums", $handle);
   

$totalthreads = 0;
$totalposts = 0;

//Sum posts and threads (Could do this in the sql statement)
while ($forumitem = mysql_fetch_array($forum))
{
  $totalthreads += $forumitem['threadcount'];
  $totalposts += $forumitem['replycount'];
}

$totalthreads = number_format($totalthreads);
$totalposts = number_format($totalposts);



echo "Members: $numbermembers" . '<br />';
echo "Total Posts: $totalposts" . '<br />';
echo "Total Threads: $totalthreads"; 
?></ul>

Gives me this result -

Code:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\wamp\www\wordpress\wp-content\themes\xeiro\sidebar_r.php on line 28

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\wamp\www\wordpress\wp-content\themes\xeiro\sidebar_r.php on line 51
Members: 0
Total Posts: 0
Total Threads: 0

Any ideas?

the database is called forums and is the root.

Thanks,

Andy
 
Changed it to what is below, and i'm still gettin the same result.

Any ideas?

Code -

Code:
<?php $handle = mysql_connect('localhost', 'root', '');

$datastoretemp = mysql_query("
            SELECT title, data
            FROM databasename.datastore
            WHERE title = 'forumcache'
                OR title = 'userstats'
        ", $handle);

        // initialise
        $forumcache = array();

        while ($storeitem = mysql_fetch_array($datastoretemp))
        {
            switch($storeitem['title'])
            {
                // get $forumcache array
                case 'forumcache':
                {
                    $forumcache = unserialize($storeitem['data']);
                }
                
                // stuff the data into the $datastore array
                default:
                {
                    $datastore["$storeitem[title]"] = $storeitem['data'];
                }
                break;
            }
        }

$totalthreads = 0;
$totalposts = 0;
if (is_array($forumcache))
{
    foreach ($forumcache AS $forum)
    {
        $totalthreads += $forum['threadcount'];
        $totalposts += $forum['replycount'];
    }
}
$totalthreads = number_format($totalthreads);
$totalposts = number_format($totalposts);

// get total members and newest member from template
$userstats = unserialize($datastore['userstats']);
$numbermembers = number_format($userstats['numbermembers']);
$newusername = $userstats['newusername'];
$newuserid = $userstats['newuserid'];

echo "Total Threads: $totalthreads" . '<br />';
echo "Total Posts: $totalposts" . '<br />';
echo "Members: $numbermembers";   
?>

Slightly shortened.
 
Last edited:
Ok i got it working with -

Code:
<?php $handle = mysql_connect('localhost', 'root', '');

$datastoretemp = mysql_query("
            SELECT title, data
            FROM forums.datastore
            WHERE title = 'forumcache'
                OR title = 'userstats'
        ", $handle);

        // initialise
        $forumcache = array();

        while ($storeitem = mysql_fetch_array($datastoretemp))
        {
            switch($storeitem['title'])
            {
                // get $forumcache array
                case 'forumcache':
                {
                    $forumcache = unserialize($storeitem['data']);
                }
                
                // stuff the data into the $datastore array
                default:
                {
                    $datastore["$storeitem[title]"] = $storeitem['data'];
                }
                break;
            }
        }

$totalthreads = 0;
$totalposts = 0;
if (is_array($forumcache))
{
    foreach ($forumcache AS $forum)
    {
        $totalthreads += $forum['threadcount'];
        $totalposts += $forum['replycount'];
    }
}
$totalthreads = number_format($totalthreads);
$totalposts = number_format($totalposts);

// get total members and newest member from template
$userstats = unserialize($datastore['userstats']);
$numbermembers = number_format($userstats['numbermembers']);
$newusername = $userstats['newusername'];
$newuserid = $userstats['newuserid'];

echo "Total Threads: $totalthreads" . '<br />';
echo "Total Posts: $totalposts" . '<br />';
echo "Members: $numbermembers";     
?>

But only getting -

Total Threads: 0
Total Posts: 0
Members: 1

I have 2 threads, 2 posts.

Any ideas?
 
Back
Top Bottom