We help IT Professionals succeed at work.

Wordpress and MySQl and PHP Error on attempt to Install UserOnline Plugin

614 Views
Last Modified: 2013-12-13
Error on attempt to install UserOnline Plugin to WordPress 2.01 :  MySQL and Error Msg Follows (Please advise):

<?php
/*
 * Useronline Plugin For WordPress
 *      - useronline-install.php
 *
 * Copyright © 2004-2005 Lester "GaMerZ" Chan
*/


// Require WordPress Config
require_once('../wp-config.php');

// Create Useronline Table
$sql[] = "CREATE TABLE $wpdb->useronline (".
  " `timestamp` int(15) NOT NULL default '0',".
  " `username` varchar(50) NOT NULL default '',".
  " `ip` varchar(40) NOT NULL default '',".
  " `location` varchar(255) NOT NULL default '',".
  " `url` varchar(255) NOT NULL default '',".
  " PRIMARY KEY  (`timestamp`),".
  " KEY `username` (`username`),".
  " KEY `ip` (`ip`),".
  " KEY `file` (`location`))";

// Run The Queries
foreach($sql as $query) {
      $wpdb->query($query);
}
?>

Error Follows:

WordPress database error: [You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near '( `timestamp` int(15) NOT NULL default '0', `username` varchar(]
CREATE TABLE ( `timestamp` int(15) NOT NULL default '0', `username` varchar(50) NOT NULL default '', `ip` varchar(40) NOT NULL default '', `location` varchar(255) NOT NULL default '', `url` varchar(255) NOT NULL default '', PRIMARY KEY (`timestamp`), KEY `username` (`username`), KEY `ip` (`ip`), KEY `file` (`location`))


 
Comment
Watch Question

Guy Hengel [angelIII / a3]Billing Engineer
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2009

Commented:
try this syntax:

// Create Useronline Table
$sql[] = "CREATE TABLE {$wpdb->useronline} (".
  " `timestamp` int(15) NOT NULL default '0',".
  " `username` varchar(50) NOT NULL default '',".
  " `ip` varchar(40) NOT NULL default '',".
  " `location` varchar(255) NOT NULL default '',".
  " `url` varchar(255) NOT NULL default '',".
  " PRIMARY KEY  (`timestamp`),".
  " KEY `username` (`username`),".
  " KEY `ip` (`ip`),".
  " KEY `file` (`location`))";

now, you might want to eacho out the $sql[] value(s) to the output to see what the value is, to see if th evalue is properly filled in.

Howard BashSenior Software Engineer

Author

Commented:
I tried the above syntax and still getting this error :

WordPress database error: [You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near '( `timestamp` int(15) NOT NULL default '0', `username` varchar(]
CREATE TABLE ( `timestamp` int(15) NOT NULL default '0', `username` varchar(50) NOT NULL default '', `ip` varchar(40) NOT NULL default '', `location` varchar(255) NOT NULL default '', `url` varchar(255) NOT NULL default '', PRIMARY KEY (`timestamp`), KEY `username` (`username`), KEY `ip` (`ip`), KEY
Guy Hengel [angelIII / a3]Billing Engineer
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2009

Commented:
as you see in the CREATE TABLE statement, the name of the table is not there.
which simply means that the value of $wpdb->useronline is empty.

get that fixed, and your create table statement will work better
Billing Engineer
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2009
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Howard BashSenior Software Engineer

Author

Commented:
The install code,  that runs the failing SQL has that require_once n(../wp_config.sys) in it already...
I tried anther approach.  I added a connection with  MySQL admin and created the table.

Now,  the pluggin gets enabled but does nothing.  I have followed the install instructions, but I only see the plugin in the admin to activate/deactive it.  But the plugin does not seem to do as expected.  It seems as though some environment settings are not there.  I did add the statement to the wp-settings.php file for the $wpdb->useronline variable (the wp-config.php inludes the wp-settings file which is where the variable is declared/set -- I think).

It is the usersonline plugin that I have used with little or no effort at other web blogs before.  I will send the pieces here :

The Readme.txt
------
-> Useronline Plugin For WordPress
--------------------------------------------------
Author      -> Lester 'GaMerZ' Chan
Email      -> lesterch@singnet.com.sg
Website      -> http://www.lesterchan.net/
Demo      -> http://www.lesterchan.net/wp-useronline.php
Updated      -> 29th April 2005
--------------------------------------------------


-> Installation Instructions
--------------------------------------------------
// Open wp-settings.php

Find:
------------------------------------------------------------------
$wpdb->postmeta                              = $table_prefix . 'postmeta';
------------------------------------------------------------------
Add Below It:
------------------------------------------------------------------
$wpdb->useronline                              = $table_prefix . 'useronline';
------------------------------------------------------------------


// Open wp-admin folder

Put:
------------------------------------------------------------------
useronline-install.php
------------------------------------------------------------------


// Open root Wordpress folder

Put:
------------------------------------------------------------------
wp-useronline.php
------------------------------------------------------------------


// Open wp-content/plugins folder

Put:
------------------------------------------------------------------
useronline.php
------------------------------------------------------------------


// Activate useronline plugin


// Run wp-admin/useronline-install.php

Note:
------------------------------------------------------------------
If You See A Blank Page Means It Is Successfully
------------------------------------------------------------------


// Open wp-content/themes/<YOUR THEME NAME>/header.php

Add:
------------------------------------------------------------------
<p align="center"><a href="wp-useronline.php"><?php get_useronline(); ?></a></p>
------------------------------------------------------------------------
------

this is the entry I made in the wp_settings.php
$wpdb->useronline      = $table_prefix . 'useronline';



------
wp_useronline.php

<?php
/*
Plugin Name: Useronline
Plugin URI: http://www.lesterchan.net/portfolio/programming.php
Description: Adds A Useronline Feature To WordPress
Version: 1.5
Author: GaMerZ
Author URI: http://www.lesterchan.net
*/


### Get IP
function get_IP() {
      if (empty($_SERVER["HTTP_X_FORWARDED_FOR"])) {
            $ip_address = $_SERVER["REMOTE_ADDR"];
      } else {
            $ip_address = $_SERVER["HTTP_X_FORWARDED_FOR"];
      }
      if(strpos($ip_address, ',') !== false) {
            $ip_address = explode(',', $ip_address);
            $ip_address = $ip_address[0];
      }
      return $ip_address;
}

### Useronline Function
function get_useronline($user = 'User', $users = 'Users', $usertimeout = 300) {
      global $wpdb;
      // Search Bots
      $bots = array('Google Bot' => 'googlebot', 'MSN' => 'msnbot', 'Alex' => 'ia_archiver', 'Lycos' => 'lycos', 'Ask Jeeves' => 'askjeeves', 'Altavista' => 'scooter', 'AllTheWeb' => 'fast-webcrawler', 'Inktomi' => 'slurp@inktomi', 'Turnitin.com' => 'turnitinbot');

      // Useronline Settings
      $timeoutseconds = $usertimeout;
      $timestamp = time();
      $timeout = $timestamp-$timeoutseconds;
      
      // Check Members
      if(isset($_COOKIE['comment_author_'.COOKIEHASH]))  {
            $memberonline = trim($_COOKIE['comment_author_'.COOKIEHASH]);
            $where = "WHERE username='$memberonline'";
      // Check Guests
      } else {
            $memberonline = 'Guest';
            $where = "WHERE ip='".get_IP()."'";
      }
      // Check For Bot
      foreach ($bots as $name => $lookfor) {
            if (stristr($_SERVER['HTTP_USER_AGENT'], $lookfor) !== false) {
                  $memberonline = addslashes($name);
                  $where = "WHERE ip='".get_IP()."'";
            }
      }
      // Update User First
      $make_page = wp_title('&raquo;', false);
      if(empty($make_page)) {
            $make_page = get_bloginfo('name');
      } else {
            $make_page = get_bloginfo('name').' &raquo; Blog Archive'.$make_page;
      }
      $update_user = $wpdb->query("UPDATE $wpdb->useronline SET timestamp = '$timestamp', ip = '".get_IP()."', location = '".addslashes($make_page)."', url = '".$_SERVER['REQUEST_URI']."' $where");
      // If No User Insert It
      if(!$update_user) {
            $insert_user = $wpdb->query("INSERT INTO $wpdb->useronline VALUES ('$timestamp', '$memberonline', '".get_IP()."', '".addslashes($make_page)."', '".$_SERVER['REQUEST_URI']."')");
      }

      $delete_users = $wpdb->query("DELETE FROM $wpdb->useronline WHERE timestamp < $timeout");
      $useronline = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->useronline");
      if($useronline > 1) {
            echo "<b>$useronline</b> $users Online";
      } else {
            echo "<b>$useronline</b> $user Online";
      }
}
?>

------
wp-useronline.php

<?php
/*
 * Useronline Plugin For WordPress
 *      - wp-useronline.php
 *
 * Copyright © 2004-2005 Lester "GaMerZ" Chan
*/


// Require WordPress Header
require('wp-blog-header.php');

// Search Bots
$bots = array('Google Bot' => 'googlebot', 'MSN' => 'msnbot', 'Alex' => 'ia_archiver', 'Lycos' => 'lycos', 'Ask Jeeves' => 'askjeeves', 'Altavista' => 'scooter', 'AllTheWeb' => 'fast-webcrawler', 'Inktomi' => 'slurp@inktomi', 'Turnitin.com' => 'turnitinbot');

// Reassign Bots Name
$bots_name = array();
foreach($bots as $botname => $botlookfor) {
      $bots_name[] = $botname;
}

// Get User Online
$usersonline = $wpdb->get_results("SELECT * FROM $wpdb->useronline");

// Type Of Users Array
$bots = array();
$guests = array();
$members = array();

// Users Count
$total = array();
$total['bots'] = 0;
$total['guests'] = 0;
$total['members'] = 0;

// Assign It To Array
foreach($usersonline as $useronline) {
      if($useronline->username == 'Guest') {
            $guests[] = array('username' => stripslashes($useronline->username), 'timestamp' => $useronline->timestamp, 'ip' => $useronline->ip, 'location' => stripslashes($useronline->location), 'url' => $useronline->url);
            $total['guests']++;
      } elseif(in_array($useronline->username, $bots_name)) {
            $bots[] = array('username' => stripslashes($useronline->username), 'timestamp' => $useronline->timestamp, 'ip' => $useronline->ip, 'location' => stripslashes($useronline->location), 'url' => $useronline->url);
            $total['bots']++;
      } else {
            $members[] = array('username' => stripslashes($useronline->username), 'timestamp' => $useronline->timestamp, 'ip' => $useronline->ip, 'location' => stripslashes($useronline->location), 'url' => $useronline->url);
            $total['members']++;
      }
}

// Nicer Text
$nicetext = array();
if($total['bots'] > 1) { $nicetext['bots'] = 'Bots'; } else {      $nicetext['bots'] = 'Bot'; }
if($total['guests'] > 1) { $nicetext['guests'] = 'Guests'; } else { $nicetext['guests'] = 'Guest'; }
if($total['members'] > 1) { $nicetext['members'] = 'Members'; } else { $nicetext['members'] = 'Member'; }

// Check IP
function check_ip($ip) {
      if(isset($_COOKIE['wordpressuser_'.COOKIEHASH])) {
            return "(<a href=\"http://ws.arin.net/cgi-bin/whois.pl?queryinput=$ip\" target=\"_blank\" title=\"".gethostbyaddr($ip)."\">$ip</a>)";
      }
}
?>
<?php get_header(); ?>
      <div id="content" class="narrowcolumn">
            <p>There Are A Total Of <b><?=$total['members'].' '.$nicetext['members']?></b>, <b><?=$total['guests'].' '.$nicetext['guests']?></b> And <b><?=$total['bots'].' '.$nicetext['bots']?></b> Online Now.<b></b> </p>
            <table width="100%" border="0" cellspacing="1" cellpadding="5">
            <?php
                        if($total['members'] > 0) {
                              echo       '<tr><td><h2 class="pagetitle">'.$total['members'].' '.$nicetext['members'].' Online Now</h2></td></tr>';
                        }
            ?>
                        <?php
                              $no=1;
                              foreach($members as $member) {
                                    echo '<tr>';
                                    echo '<td><b>#'.$no.' - <a href="wp-stats.php?author='.$member['username'].'">'.$member['username'].'</a></b> '.check_ip($member['ip']).' on '.gmdate('d.m.Y @ H:i',($member['timestamp']+(get_settings('gmt_offset') * 3600))).'<br />'.$member['location'].' [<a href="'.$member['url'].'">url</a>]</td>'."\n";
                                    echo '</tr>';
                                    $no++;
                              }
                              // Print Out Guest
                              if($total['guests'] > 0) {
                                    echo       '<tr><td><h2 class="pagetitle">'.$total['guests'].' '.$nicetext['guests'].' Online Now</h2></td></tr>';
                              }
                              $no=1;
                              foreach($guests as $guest) {
                                    echo '<tr>';
                                    echo '<td><b>#'.$no.' - '.$guest['username'].'</b> '.check_ip($guest['ip']).' on '.gmdate('d.m.Y @ H:i',($guest['timestamp']+(get_settings('gmt_offset') * 3600))).'<br />'.$guest['location'].' [<a href="'.$guest['url'].'">url</a>]</td>'."\n";
                                    echo '</tr>';
                                    $no++;
                              }
                              // Print Out Bots
                              if($total['bots'] > 0) {
                                    echo       '<tr><td><h2 class="pagetitle">'.$total['bots'].' '.$nicetext['bots'].' Online Now</h2></td></tr>';
                              }
                              $no=1;
                              foreach($bots as $bot) {
                                    echo '<tr>';
                                    echo '<td><b>#'.$no.' - '.$bot['username'].'</b> '.check_ip($bot['ip']).' on '.gmdate('d.m.Y @ H:i',($bot['timestamp']+(get_settings('gmt_offset') * 3600))).'<br />'.$bot['location'].' [<a href="'.$bot['url'].'">url</a>]</td>'."\n";
                                    echo '</tr>';
                                    $no++;
                              }
                        ?>
                        </table>
      </div>
<?php
      get_sidebar();
      get_footer();
?>
------
useronline.php

<?php
/*
Plugin Name: Useronline
Plugin URI: http://www.lesterchan.net/portfolio/programming.php
Description: Adds A Useronline Feature To WordPress
Version: 1.5
Author: GaMerZ
Author URI: http://www.lesterchan.net
*/


### Get IP
function get_IP() {
      if (empty($_SERVER["HTTP_X_FORWARDED_FOR"])) {
            $ip_address = $_SERVER["REMOTE_ADDR"];
      } else {
            $ip_address = $_SERVER["HTTP_X_FORWARDED_FOR"];
      }
      if(strpos($ip_address, ',') !== false) {
            $ip_address = explode(',', $ip_address);
            $ip_address = $ip_address[0];
      }
      return $ip_address;
}

### Useronline Function
function get_useronline($user = 'User', $users = 'Users', $usertimeout = 300) {
      global $wpdb;
      // Search Bots
      $bots = array('Google Bot' => 'googlebot', 'MSN' => 'msnbot', 'Alex' => 'ia_archiver', 'Lycos' => 'lycos', 'Ask Jeeves' => 'askjeeves', 'Altavista' => 'scooter', 'AllTheWeb' => 'fast-webcrawler', 'Inktomi' => 'slurp@inktomi', 'Turnitin.com' => 'turnitinbot');

      // Useronline Settings
      $timeoutseconds = $usertimeout;
      $timestamp = time();
      $timeout = $timestamp-$timeoutseconds;
      
      // Check Members
      if(isset($_COOKIE['comment_author_'.COOKIEHASH]))  {
            $memberonline = trim($_COOKIE['comment_author_'.COOKIEHASH]);
            $where = "WHERE username='$memberonline'";
      // Check Guests
      } else {
            $memberonline = 'Guest';
            $where = "WHERE ip='".get_IP()."'";
      }
      // Check For Bot
      foreach ($bots as $name => $lookfor) {
            if (stristr($_SERVER['HTTP_USER_AGENT'], $lookfor) !== false) {
                  $memberonline = addslashes($name);
                  $where = "WHERE ip='".get_IP()."'";
            }
      }
      // Update User First
      $make_page = wp_title('&raquo;', false);
      if(empty($make_page)) {
            $make_page = get_bloginfo('name');
      } else {
            $make_page = get_bloginfo('name').' &raquo; Blog Archive'.$make_page;
      }
      $update_user = $wpdb->query("UPDATE $wpdb->useronline SET timestamp = '$timestamp', ip = '".get_IP()."', location = '".addslashes($make_page)."', url = '".$_SERVER['REQUEST_URI']."' $where");
      // If No User Insert It
      if(!$update_user) {
            $insert_user = $wpdb->query("INSERT INTO $wpdb->useronline VALUES ('$timestamp', '$memberonline', '".get_IP()."', '".addslashes($make_page)."', '".$_SERVER['REQUEST_URI']."')");
      }

      $delete_users = $wpdb->query("DELETE FROM $wpdb->useronline WHERE timestamp < $timeout");
      $useronline = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->useronline");
      if($useronline > 1) {
            echo "<b>$useronline</b> $users Online";
      } else {
            echo "<b>$useronline</b> $user Online";
      }
}
?>
------
useronline-install.php <---This is the program that failed and generated the initial error message.  I manually created the table and assumed that the plugin would work.

<?php
/*
 * Useronline Plugin For WordPress
 *      - useronline-install.php
 *
 * Copyright © 2004-2005 Lester "GaMerZ" Chan
*/


// Require WordPress Config
require_once('../wp-config.php');

// Create Useronline Table
$sql[] = "CREATE TABLE {$wpdb->useronline} (".
  " `timestamp` int(15) NOT NULL default '0',".
  " `username` varchar(50) NOT NULL default '',".
  " `ip` varchar(40) NOT NULL default '',".
  " `location` varchar(255) NOT NULL default '',".
  " `url` varchar(255) NOT NULL default '',".
  " PRIMARY KEY  (`timestamp`),".
  " KEY `username` (`username`),".
  " KEY `ip` (`ip`),".
  " KEY `file` (`location`))";

// Run The Queries
foreach($sql as $query) {
      $wpdb->query($query);
}
?>
------

It is a business blog, so I am trying to be extremely cautious with my experiments / "enhancements".

Please advise.

Thanks,
Howard
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.