Any PHP masters on here?
Author
Discussion

tegwin

Original Poster:

1,682 posts

236 months

Wednesday 31st March 2010
quotequote all
Have a bit of an issue... wonder if anyone here has any ideas?!?



I have a function simlar to this: "getUserDetails($userID)" That returns an array of details for that given user.

User ID is a 4 or 5 digit number.

What I want to do is write some code that runs the "getUserDetails()" function repeatedly but each time it does it with a sequentially different user ID... so numerically starts at 0001 and runs up to 20000

The returned data for each user will then be entered into a database table before moving on to the next user ID and so on until it reaches 20000

Can anyone suggest a way of doing this with php? :dubious:

evenflow

8,883 posts

312 months

Wednesday 31st March 2010
quotequote all
for ($i=0; $i<20000; $i++)
{
$userDetailsArray = array();
$userDetailsArray = getUserDetails($i);
// Do the database insert, e.g.
$q = "INSERT INTO users (user_id, user_name) VALUES (" . $userDetailsArray['id'] . ", '" . $userDetailsArray['username'] . "')";
$r = mysql_query($r);
}

Edited by evenflow on Wednesday 31st March 10:29

evenflow

8,883 posts

312 months

Wednesday 31st March 2010
quotequote all
One more thing...

You say userID is a 4 or 5 digit number. Is it stored in the DB as an integer? If so, code above will be fine, if not (and it is stored as a string) you may need to pad the userID with zeroes.


American iv

468 posts

226 months

Wednesday 31st March 2010
quotequote all
what does the function do? It might be far faster to replicate the functionality than it is to use a for loop, e.g.

if getUserDetails() simply runs a SQL statement and converts it into an array and then gives it to the return, why not:

insert into <NEWTABLE> (`<FIELDS OF NEW TABLE>')
select <FIELDS FROM OLD TABLE WHICH MATCH NEW TABLE>
from <OLDTABLE>
where userid > 0
and userid < 2000


Of course, depending on what you need to do, you could always use a Database view to prepare the data from the raw table...

tegwin

Original Poster:

1,682 posts

236 months

Wednesday 31st March 2010
quotequote all
Good call Evanflow smile That seems to work a treat!

Bit of a bugger I can only do 500 Id's at a time... the server seems to have a very short time limit set for page excecution... and sadly I cant change that!!

Looks like I will have to call the same page multiple times to load up chunks of data in different ranges... Not the end of the world smile



@American... I dont have access to the underlying database which is a pain.. hence why I am trying to create my own database smile

Edited by tegwin on Wednesday 31st March 11:58

Xenocide

4,286 posts

238 months

Wednesday 31st March 2010
quotequote all
You can run the ini_set() function to set the execution time. Like

 ini_set('max_execution_time', 300); //300 seconds = 5 minutes

tegwin

Original Poster:

1,682 posts

236 months

Wednesday 31st March 2010
quotequote all
Xenocide said:
You can run the ini_set() function to set the execution time. Like

 ini_set('max_execution_time', 300); //300 seconds = 5 minutes
Good call... but ini_set() is disabled on my system and I dont have access to enable it frown

evenflow

8,883 posts

312 months

Wednesday 31st March 2010
quotequote all
Max page execution time is usually set to 30s
You could have a few different scripts running in a few different browsers...

EG

script1.php has the for loop going from 0-1000
script2.php has the for loop going from 1001-2000
etc...

load them up in seperate browser windows and run...

john_p

7,073 posts

280 months

Wednesday 31st March 2010
quotequote all
set_time_limit() does the same thing

put a flush() at the end of the for loop if you are outputting text, so your browser won't hang mysteriously while it's processing

also, IE hates it if you have loads and loads of text on a page.


Edited by john_p on Wednesday 31st March 13:12

evenflow

8,883 posts

312 months

Wednesday 31st March 2010
quotequote all
john_p said:
set_time_limit() does the same thing
^ or do that smile