mySQL
Moderator: General Moderators
mySQL
i got mySQL server running..
Create MySQL Table...
#
# Table structure for table 'useronline'
#
CREATE TABLE useronline (
timestamp int(15) DEFAULT '0' NOT NULL,
ip varchar(40) NOT NULL,
file varchar(100) NOT NULL,
PRIMARY KEY (timestamp),
KEY ip (ip),
KEY file (file)
);
how do I create thhose??
Create MySQL Table...
#
# Table structure for table 'useronline'
#
CREATE TABLE useronline (
timestamp int(15) DEFAULT '0' NOT NULL,
ip varchar(40) NOT NULL,
file varchar(100) NOT NULL,
PRIMARY KEY (timestamp),
KEY ip (ip),
KEY file (file)
);
how do I create thhose??
2 ways:
1.) If you have phpMyAdmin then you can do it this way. If you don't have phpMyAdmin skip to the 2nd one.
Ok, open up phpMyAdmin select the database you want from the left frame. Then press the Query Window link at the bottom left. Paste the above code into the query window and press Go.l
2.) You will have to make a PHP page to install it. Replace the db values with whatever you username and password are:
1.) If you have phpMyAdmin then you can do it this way. If you don't have phpMyAdmin skip to the 2nd one.
Ok, open up phpMyAdmin select the database you want from the left frame. Then press the Query Window link at the bottom left. Paste the above code into the query window and press Go.l
2.) You will have to make a PHP page to install it. Replace the db values with whatever you username and password are:
Code: Select all
<?php
$host = "localhost";
// replace these with your username, password and dbname
$user = "dbUsername";
$password = "dbPassword";
$db = "dbName";
@mysql_connect($host,$user,$password) or die("Unable to connect to MySQL database: " . mysql_error());
@mysql_select_db($db) or die( "Unable to select MySQL database.");
$query = "CREATE TABLE useronline (
timestamp int(15) DEFAULT '0' NOT NULL,
ip varchar(40) NOT NULL,
file varchar(100) NOT NULL,
PRIMARY KEY (timestamp),
KEY ip (ip),
KEY file (file)
);"
$result = mysql_query($query) or die("MySQL query failed: " . mysql_error());
echo "MySQL table created successfully."
?>THank you.. I found it. Do I need anything after these?
Should I deleted Createmysql table in phpfile?
Should I deleted Createmysql table in phpfile?
Code: Select all
Create MySQL Table...
#
# Table structure for table 'useronline'
#
CREATE TABLE useronline (
timestamp int(15) DEFAULT '0' NOT NULL,
ip varchar(40) NOT NULL,
file varchar(100) NOT NULL,
PRIMARY KEY (timestamp),
KEY ip (ip),
KEY file (file)
);
Create Script...
<?php
$server = "xxx";
$db_user = "xxx";
$db_pass = "xxx";
$database = "xxx";
$timeoutseconds = 300;
$timestamp = time();
$timeout = $timestamp-$timeoutseconds;
mysql_connect($server, $db_user, $db_pass);
$insert = mysql_db_query($database, "INSERT INTO useronline VALUES ('$timestamp','$REMOTE_ADDR','$PHP_SELF')");
if(!($insert)) {
print "Useronline Insert Failed > ";
}
$delete = mysql_db_query($database, "DELETE FROM useronline WHERE timestamp<$timeout");
if(!($delete)) {
print "Useronline Delete Failed > ";
}
$result = mysql_db_query($database, "SELECT DISTINCT ip FROM useronline WHERE file='$PHP_SELF'");
if(!($result)) {
print "Useronline Select Error > ";
}
$user = mysql_num_rows($result);
mysql_close();
if($user == 1) {
print("<b>$user</b> user online\n");
} else {
print("<b>$user</b> users online\n");
}
?>Yes, just delete the text starting with:
Create MySQL Table...
down to
Create Script...
Or, if you want to keep it you can just add a above Create MySQL Table... and a after Create Script...
Create MySQL Table...
down to
Create Script...
Or, if you want to keep it you can just add a
Code: Select all
/*Code: Select all
*/Just adding;
Note the 'IF NOT EXISTS'. Probably selfexplainatory.
Code: Select all
CREATE TABLE IF NOT EXISTS useronline (
timestamp int(15) DEFAULT '0' NOT NULL,
ip varchar(40) NOT NULL,
file varchar(100) NOT NULL,
PRIMARY KEY (timestamp),
KEY ip (ip),
KEY file (file)
);-
microthick
- Forum Regular
- Posts: 543
- Joined: Wed Sep 24, 2003 2:15 pm
- Location: Vancouver, BC
Few points:
You've declared key and file to be alternative keys. But I don't think this should be. What if two different users visit the same page. Or what if one user visits more than one page.
Errors would probably occur.
Also, you're inserting $timestamp into an INT field. Are you sure you can do that? Will the INT field accept characters like ":" and "-" and spaces?
As well, you're assigning memory of INT(15) ... that's enough bit space to hold integers up to 256^15 (or some ungodly number like that.. I think it's actually 2^120). Do you really need that much space?
You've declared key and file to be alternative keys. But I don't think this should be. What if two different users visit the same page. Or what if one user visits more than one page.
Errors would probably occur.
Also, you're inserting $timestamp into an INT field. Are you sure you can do that? Will the INT field accept characters like ":" and "-" and spaces?
As well, you're assigning memory of INT(15) ... that's enough bit space to hold integers up to 256^15 (or some ungodly number like that.. I think it's actually 2^120). Do you really need that much space?