...

View Full Version : php/MySQL request.



colabus
06-02-2004, 02:17 AM
I'm sort of new to these languages, I have been doing php for a couple of weeks and finally got an idea how to use MySQL just 2 days ago. Anyhow I'm building a script that reads a file (line-by-line), sets 4 variables ($date, $ip, $time and $name) and I want to build them into a database. Something like:

Database: ipLOG
Table: $date, then 3 columns ($time, $name and $ip)

It's actually storing iplogs from a game server I run.
Well I got the vars set and can loops through the file fine, it's just the mysql side. I need to 1. check the the table ($date) exists, and if not make it, 2. then check if there's any matches for $ip, $name, $time and if not put them into the database (so it doesn't put duplicates in).

here's what I have (mysql wise) so far,


<?
$username=username;
$password=password;
$database=ipLOG;
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die("Database failure.."); //this is all fine

//now the loop starts (I didn't paste that tho)

$query = "CREATE IF NOT EXISTS TABLE $date (
time varchar(5) NOT NULL,
name varchar(20) NOT NULL,
ip varchar(15) NOT NULL
)";

mysql_query($query); //if no table, build it

$query="SELECT * FROM $date WHERE time='$time' AND name='$name' AND ip='$ip'";

$result=mysql_query($query); // check if there's a row with the set info (so no duplicates)

$num=mysql_numrows($result); //sets num, assuming there is a match $num will exist.

if (!$num) {
$query="INSERT INTO $date VALUES ('','$time','$name','$ip')";
mysql_query($query); //if $num didn't exist, write the info
}

//loop ends

mysql_close();
?>


if anyone could help, it would be great.
thanks

MrShed
06-02-2004, 09:18 PM
well are you actually having any problems with it so far? is there like a specific question you want answered or just general help?

colabus
06-03-2004, 05:50 AM
I ended up working out the problem.

Seems tables can't start with a numbers or have dashes in their names



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum