TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 05-15-2009, 11:46 AM   #1 (permalink)
The Contributor
 
Join Date: Mar 2009
Posts: 49
Thanks: 0
TheOnly92 is on a distinguished road
Default Help on Custom Data Storage

I need a custom data storage function which stores data on a text file. I need to make it only update 1 line at a time because I am having heavy requests and it needs to make several edits in a second.

I know it will be easier using database, but I (insist that I) need to use custom data storage function. Can anyone help please?
TheOnly92 is offline  
Reply With Quote
Old 05-15-2009, 01:07 PM   #2 (permalink)
The Addict
 
Enfernikus's Avatar
 
Join Date: Jun 2008
Posts: 335
Thanks: 2
Enfernikus is on a distinguished road
Default

If you INSIST on using a file based data storage system ( Which will grow to be a slow heavy fiend after a while ) use XML or if you want a purely text file type storage system ( waste of time ) than you can use serialize/unserialize and use line # as a type of ID
Enfernikus is offline  
Reply With Quote
Old 05-15-2009, 02:45 PM   #3 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

Just delve into the lovely filesystem functions. Without any specific issues, problems, questions, we can't really be of any use to you.

If we were to start providing help on this topic, it could end up being useless since we don't know what you want/require/need. So, give us some context and ask some specific questions.
Salathe is offline  
Reply With Quote
Old 05-15-2009, 03:34 PM   #4 (permalink)
how quixotic are you?
 
ETbyrne's Avatar
 
Join Date: Dec 2007
Location: Lapeer, MI
Posts: 445
Thanks: 37
ETbyrne is on a distinguished road
Default

I would suggest not using XML, simply because it can be a pain to parse. Instead I would store the data in JSON format.

Although, it is best to use a SQL database. They are much faster and handle big databases quickly.
__________________
Dingo Web Systems > http://www.dingocode.com
My Website > http://www.evanbot.com
ETbyrne is offline  
Reply With Quote
Old 05-15-2009, 05:13 PM   #5 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

If you don't mind my asking, why can't you use a database? There is -no- advantage to using a text file if you are working with either high volume traffic or lots of data.

Past that we need to know what you are doing to advise you on a custom data format.
__________________

Village Idiot is offline  
Reply With Quote
Old 05-15-2009, 09:09 PM   #6 (permalink)
The Contributor
 
Join Date: Feb 2007
Posts: 64
Thanks: 9
Killswitch is on a distinguished road
Default

I would use an XML file and use SimpleXML to retrieve the data, it's extremely easy. I store my page cache in XML files and its as easy as...

$xml->tag_name to grab the data. Ofcoarse, it can get a bit more complicated for nested items, but even that is pretty simple.

See :
http://us2.php.net/manual/en/functio...-load-file.php
http://us2.php.net/manual/en/functio...oad-string.php
Killswitch is offline  
Reply With Quote
Old 05-15-2009, 10:57 PM   #7 (permalink)
The Addict
 
CoryMathews's Avatar
 
Join Date: Nov 2007
Location: USA
Posts: 256
Thanks: 7
CoryMathews is on a distinguished road
Default

XML for sure, there are some good parsers out there that will be able to index it fairly quickly. Also when using XML you will later be able to easily import it all into a database when your files get to large.
CoryMathews is offline  
Reply With Quote
Old 05-15-2009, 11:40 PM   #8 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

Quote:
Originally Posted by CoryMathews View Post
Also when using XML you will later be able to easily import it all into a database when your files get to large.
That's not really an issue is it? It is fairly trivial to import any data source "into a database"—there is nothing special about XML in that respect.

I still invite the OP to post a little more about what they want, why and any specific questions they have.
Salathe is offline  
Reply With Quote
Old 05-16-2009, 06:39 AM   #9 (permalink)
The Contributor
 
Join Date: Mar 2009
Posts: 49
Thanks: 0
TheOnly92 is on a distinguished road
Default

Ok, that's awful lots of replies at a time. I will answer the question 1 by 1:

@Enfernikus:
I'm already using serialize/unserialize, but the down side I see is that variables updated in a session gets overwritten by another session later on. Understand what I mean?

@Salathe:
I'm actually testing this er "method" or idea before implementing into an application. That application's requirements needs to stay basic which means the user only needs a web server and PHP to make it work. Therefore I'm trying my best to avoid using database.

@ETByrne:
Well, I really need a format that stores 1 row of array at 1 line, so that whenever I need to update them, I just update that particular line without touching the rest, unless PHP is unable to handle them...

@Village_Idiot:
Look at my answer to Salathe.
I'm only storing a few things, because it's about a script to download some files to the server, so it only needs to store the filename, the id, the download speed, currently downloaded file size and the total file size. I hope I'm clear enough for you guys to answer...

Seems like a lot of people recommend XML, I'll have a look at it first.

If it's still unclear to some of you what I actually want, here's a list of it
1. I only need to store a few strings and few numbers, but in rows, which means I will be retrieving it in 2 dimension arrays.
2. It's best if I can only update 1 line of the file and not touch the others.
3. The datas will be deleted after a period of time so the file will basically not grow too large, but the fact is that it will get updated very often (almost 1 time per 1.5s per user) which means I really need a way where I can update it without affecting other values.

Really long post but hopefully I'm clear enough.
TheOnly92 is offline  
Reply With Quote
Old 05-16-2009, 07:54 PM   #10 (permalink)
The Addict
 
Enfernikus's Avatar
 
Join Date: Jun 2008
Posts: 335
Thanks: 2
Enfernikus is on a distinguished road
Default

It sounds like you need to look into SQLite or Sessions
Enfernikus is offline  
Reply With Quote
Old 05-17-2009, 03:24 AM   #11 (permalink)
The Contributor
 
Join Date: Mar 2009
Posts: 49
Thanks: 0
TheOnly92 is on a distinguished road
Default

I will actually like to know more about sessions. Is session being updated globally (I mean user's) when a script hasn't finish executing?

EDIT: I will prefer not to use SQLite because it requires the user to install an extension, I want to run it as native as possible.
TheOnly92 is offline  
Reply With Quote
Old 05-17-2009, 04:19 AM   #12 (permalink)
The Addict
 
Enfernikus's Avatar
 
Join Date: Jun 2008
Posts: 335
Thanks: 2
Enfernikus is on a distinguished road
Default

Sessions persist until the user leaves the page ( I believe ) or until you destroy them
Enfernikus is offline  
Reply With Quote
Old 05-17-2009, 07:00 AM   #13 (permalink)
The Contributor
 
Join Date: Mar 2009
Posts: 49
Thanks: 0
TheOnly92 is on a distinguished road
Default

You might need to make it clearer, like I have a php script still running and it's updating the session, then if I initiate another script (like say the first script was index.php, then the second was get.php) will it get the updated session or the one before it?
TheOnly92 is offline  
Reply With Quote
Old 05-18-2009, 05:35 AM   #14 (permalink)
The Contributor
 
Join Date: Mar 2009
Posts: 49
Thanks: 0
TheOnly92 is on a distinguished road
Default

Ok, I have tested the sessions method and proved that it doesn't work. I created 2 files as below:

Code:
<?php
session_start();

set_time_limit(0);
for (;;) {
	$i++;
	$_SESSION['test'] = $i;
}
?>
This is the code that modifies the session variable. Another file to retrieve it here:
Code:
<?php
session_start();
var_dump($_SESSION['test']);
?>
Assuming it should work, I ran the first file, then wait a few seconds, and tried to access the second file. But the second file just kept loading and seems like it will never stop. Seems like it means its not applicable in my circumstances.

Any more ideas other than XML ?
TheOnly92 is offline  
Reply With Quote
Old 05-18-2009, 08:14 AM   #15 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

Quote:
EDIT: I will prefer not to use SQLite because it requires the user to install an extension, I want to run it as native as possible.
I was under the impression SQLite was enabled by default in php, ie.e it comes pre-bundled.
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
sketchMedia is offline  
Reply With Quote
Old 05-18-2009, 08:18 AM   #16 (permalink)
The Contributor
 
Join Date: Mar 2009
Posts: 49
Thanks: 0
TheOnly92 is on a distinguished road
Default

Hmm, I have actually no knowledge at all about SQLite. Does it need configuration? Does it come with every shared hosts?
TheOnly92 is offline  
Reply With Quote
Old 05-18-2009, 08:48 AM   #17 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

Quote:
Does it need configuration?
Not really, just needs to be enabled (which mostly it is)

Quote:
Does it come with every shared hosts?
No idea, I wasn't under the impression this was to be a distributed script. However seen as the PHP manual says its enabled by default, I'm assuming it more than likely will be, I could be wrong ofc.

Sample sqlite script:
PHP Code:
try
{
    
$db = new SQLiteDatabase('sqlite.db');
    
$r $db->arrayQuery('SELECT * from tbl WHERE col = 2');
    
var_dump($r);
}
catch (
SQLiteException $e)
{
    die(
$e->getMessage());

It can even do transactions:
PHP Code:
try
{
    
$db = new SQLiteDatabase('sqlite.db');
    
$db->query('BEGIN;
        CREATE TABLE test ( id UNSIGNED MEDIUMINT(8) PRIMARY KEY, data TEXT NOT NULL);
        INSERT INTO test (data) VALUES(\'Data\');
        INSERT INTO test (data) VALUES(\'Data2\');
        COMMIT;'
);

    echo 
$db->changes() . ' Rows inserted.'//will echo '2 Rows inserted.
}
catch (
SQLiteException $e)
{
    die(
$e->getMessage());

__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
sketchMedia is offline  
Reply With Quote
Old 05-18-2009, 09:20 AM   #18 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

SQLite is available, and enabled by default, as of PHP 5. No doubt some hosting providers will disable it for some unfathomable reason but that should only be a small portion of hosts. For PHP 4, there is a PECL extension available.
Salathe is offline  
Reply With Quote
Old 05-18-2009, 09:54 AM   #19 (permalink)
The Contributor
 
Join Date: Mar 2009
Posts: 49
Thanks: 0
TheOnly92 is on a distinguished road
Default

Ok, SQLite seems nice, I will have a try. Just in case it doesn't work (in most cases, I think it doesn't) is there anymore alternatives? I'm afraid I'm gonna need to dry my brain cells trying to think up another way.
TheOnly92 is offline  
Reply With Quote
Old 05-18-2009, 10:28 AM   #20 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

You could always go back to the beginning of the topic and create your own custom storage method.
Salathe is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
SQL Injection and mysql_real_escape_string Durux General 61 01-29-2013 12:20 PM
Venerable methods and the applications they are commonly trusted in. Village Idiot Tips & Tricks 7 11-06-2008 07:36 AM
Creating a table from loaded data benton General 5 04-20-2008 11:48 AM
The act of sharing your data Wildhoney General 0 12-06-2007 03:31 PM
Tips: PHP security Village Idiot Tips & Tricks 22 11-23-2007 11:17 PM


All times are GMT. The time now is 02:52 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design