Forum Moderators: coopster

Message Too Old, No Replies

Extracting certain strings from a file into mysql

an ambitious thing :)

         

Nostradamus

12:47 pm on May 19, 2004 (gmt 0)

10+ Year Member



Hi there, this is my first post in here...seems like a realy cool place.

I am working on this site for a game, and I hope there is a way to do the following:

I have an ini file with all the info from the game.
I want to take certain strings, which has got certain words in them, and put them(preferably only the word of the player or so, and not the entire string) in a spcific mysql table.
Then a thin I'm not at all sure about how to do, is wether it is possible for the msql to update, let's say each hour or so...so that it extracts any new values.

If this is not at all possible, could you direct me to a other method that php, where it might be doable?

I really have no clue how to tackle this, so any help will be greatly apprecieted!

Thanks in advance.

httpwebwitch

4:54 pm on May 19, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Your solution will need several components, each of which you'll need to learn. They are valuable skills, and you won't regret taking the time to master them. I'll list the built-in PHP functions that will help you along; look for their syntax and examples of their use at php.net

File functions. Use these to open the INI file and read it into your script as a long string, which can then be parsed using a regular expression.
See: fopen(), fgets()

Regular Expressions, a.k.a. "regex", "eregi", "preg", etc. These can find pattern matches in your ini file, and create an array of the "matches".
See: preg_match_all()

SQL - in addition to the PHP functions, you will be using SQL commands like SELECT, INSERT and UPDATE to write your strings to the database.
See: mysql_connect(), mysql_query()

When it's time to update your mySQL table, choose your preferered method:
1) DELETE the whole mySQL table and INSERT everything again from empty, or
2) Use a combination of DELETE, SELECT, INSERT and UPDATE. Loop through the old values and DELETE if they are not found in the new collection. Then loop thgough the new values, SELECT them, UPDATE fields if they've changed since the last time, or INSERT if it's not found.
See: mysql_num_rows(), mysql_insert_id()

CronJobs - this is a method of performing scheduled jobs, as you mention, once per hour.

Cronjobs are tricky for a beginner. I rarely use them except when no other option is feasible. It is usually easier to trigger a script using some frequent user action, such as logging in, requesting a page, etc. Create your script using the methods described above, so that it does its duty without producing any output. Then include() it on some page that is likely to be visited before it's needed.

For example, I have a series of pages that create temporary files on the server. I also have a script that tidies up the server and deletes old temporary files when they are no longer needed. I include() that clean-up script on the same page where the new files are created, thus it's a self-cleaning system that doesn't require scheduled maintenance.

You can probably use the same technique in your situation, and avoid CronJobs.

good luck!

Nostradamus

7:17 pm on May 19, 2004 (gmt 0)

10+ Year Member



Woah thank you!

A lot of useful info.... amazing :)

the fopen and preg match seems fairly easy, but ill ask something that migt seem a bit noob, but whatever :)

Let's say that i gat the file into my document and i created an array with all the names....how do I get about putting them in the sql... i know how to do it by a submit button, but that is not really what I am looking for as you can imagine :)

Many thanks again!

I know for sure I'll be hanging out here more often :)

jatar_k

8:00 pm on May 19, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld Nostradamus,

once you have run through the file and extracted all of the data you need and popped it into an array you can then use that array to do your mysql inserts.

I don't know the exact data or db structure and there is never 1 answer to any problem but I would imagine you could

loop through the array
with each iteration you could build an insert query
then insert the data
repeat until the whole array has been processed

also take a look at the PHP Library [webmasterworld.com]. There are a few threads in there dealing with file processing and insert/select from mysql

Nostradamus

11:30 pm on May 19, 2004 (gmt 0)

10+ Year Member



Thanks Jatar, that seem pretty straight forward, and the php library has an eminent thread on the matter....amazing how far i have gotten on so litle time! yey :)

Nostradamus

6:11 pm on Jun 10, 2004 (gmt 0)

10+ Year Member



okay i have gt the guild.ini file and well the setup is kindof different that i had imagined:

[r0xx0rnation]
leader=ddd
rank=0
members=2
list=ddd,mick,
m_ddd=lord,level3
m_mick=d00d,level1

Now whhat i would like to extract in different arrays is

guildname
leader
members
characters of the members and their levels

list shows the members
and the m_ shows the character names.

I got it all into a string, i just find it difficult to seperate them.
Any ideas?