[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2007-10-06
5
Medium Priority
?
571 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`))


 
0
Comment
Question by:Howard Bash
  • 3
  • 2
5 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 20028289
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.

0
 
LVL 1

Author Comment

by:Howard Bash
ID: 20028946
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
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 20029293
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
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 1500 total points
ID: 20029295
with "get that fixed", you have probably to look into:
require_once('../wp-config.php');
0
 
LVL 1

Author Comment

by:Howard Bash
ID: 20030685
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
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Backups and Disaster RecoveryIn this post, we’ll look at strategies for backups and disaster recovery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses
Course of the Month18 days, 9 hours left to enroll

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question