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
 
PHP:
while ($storeitem = mysql_fetch_array($datastoretemp))


PHP:
while ($forumitem = mysql_fetch_array($forumtemp))

You've forgot to change the while loops to reflect your query names where you have added temp to them.
 
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?
 
Soemthing to do with these 2 lines pehaps?

$totalthreads = 0;
$totalposts = 0;

Would they not be telling the script that those 2 values are 0?

Sorry new to PHP myself...
 
Soemthing to do with these 2 lines pehaps?

$totalthreads = 0;
$totalposts = 0;

Would they not be telling the script that those 2 values are 0?

Sorry new to PHP myself...

Thats just initialising the value, the problem is the for loop should be increasing their values.

I guess you could just try printing forumcache to see what youve actually got in the way of keys and values and see if its what you expected. The unserialise might not be doing what you think.
 
PHP:
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;
            }
        }
This line...

$datastore["$storeitem[title]"] = $storeitem['data'];

Every other reference is $datastoretemp, missed a line perhaps?

It's handy to turn on debugging for fixing these kind of things, so you can see the queries being executed and any errors.

Or just echo a few functions to see what they are returning.

Is this for use with a particular kind of forum or one you've made, there's usually scripts already out there for the popular forums like phpbb, vbulletin, invision, etc.... you just adjust a few lines.
 
Last edited:
Back
Top Bottom