dodgeitorelse Posted August 30, 2011 Share Posted August 30, 2011 I have some code that is supposed to get data from a site and then insert the gathered data into a database. The code does start adding data to database but it isn't adding data that it is getting from site that has starting data. What I mean is this. This code gets current player scores and such from gametracker and adds that to the database. then it will get player scores from my own tracker and add new scores to the ones that weere adde3d from game tracker. We did this so that our game servers continue tracking the scores for our new server trackers and not start out at zero initially. How ever when we run the code it says data was not written to database probably because server does not exists in database when in fact it really does. SO if someone could explain the code to me as to what it is doing then maybe I can figure out why it isn't working. function InitializeServer( $sid, $ip, $name, &$totalplayers ) { !mysql_query( "DROP TABLE ".SERVER_TABLE_NAME.$sid.";" ); // we don't care if this fails if table didn't exist before if( !mysql_query( "CREATE TABLE ".SERVER_TABLE_NAME.$sid." (" ." `name` varchar(32), `score` int, `goal` int default 0, `leader` int default 0, `enemy` int default 0, `kia` int default 0, `roe` int default 0," ." PRIMARY KEY(`name`) );" ) ) { die( "Error creating table: ".mysql_error() ); } $id = 1; $page = 1; $done = FALSE; while( !$done ) { $data = http_request( "www.gametracker.com", "/server_info/".$ip."/top_players/?searchipp=50&searchpge=$page" ); $pos = 0; $out = ""; while( !$done ) { $num = GetStringInMiddle( $data, $pos, '<td class="c01">', '</td>' ); if( $num == NULL && $id > 1 ) break; if( $num != $id || $num == NULL ) { $done = TRUE; break; } $name = GetStringInMiddle( $data, $pos, '/">', '</a>' ); if( $name === NULL ) break; $score = GetStringInMiddle( $data, $pos, '<td class="c04">', '</td>' ); //$timePlayed = GetStringInMiddle( $data, $pos, '<td class="c05">', '</td>' ); //$scorePerMinute = GetStringInMiddle( $data, $pos, '<td class="c06">', '</td>' ); if( $out != "" ) $out .= ",\n"; $out .= "('$name', '$score')"; $id ++; $totalplayers ++; } // Write data to database, if any if( $out != "" ) { $cmd = "INSERT INTO ".SERVER_TABLE_NAME.$sid." (`name`, `score`) VALUES ".$out.";"; if( !mysql_query( $cmd ) ) { echo "Error with insert: ".mysql_error()."\n[$cmd]"; } } echo ($id-1)." "; flush(); ob_flush(); $page++; } if( $id > 1 ) echo "\nInitalized server #$sid: ".$ip." - ".$name." - ".($id-1)." players added<br />"; else echo "\nServer #$sid: ".$ip." - ".$name." - no players added (server probably not in database)<br />"; flush(); ob_flush(); } $sid, $ip and such are defined in other files that are included in the same file as this code is included in. Quote Link to comment https://forums.phpfreaks.com/topic/246063-can-you-explain-this-code/ Share on other sites More sharing options...
xyph Posted August 30, 2011 Share Posted August 30, 2011 So, you're not a programmer and you'd like us to debug the code for you? Quote Link to comment https://forums.phpfreaks.com/topic/246063-can-you-explain-this-code/#findComment-1263700 Share on other sites More sharing options...
dodgeitorelse Posted August 30, 2011 Author Share Posted August 30, 2011 no I don't want you to fix it. I am just looking for an explanation as to what this code is actually doing. Quote Link to comment https://forums.phpfreaks.com/topic/246063-can-you-explain-this-code/#findComment-1263701 Share on other sites More sharing options...
dodgeitorelse Posted August 30, 2011 Author Share Posted August 30, 2011 and no I am not a programmer. I am currently learning to code. I have learned some basics. For example I can query databases, I can find my way through modifying a lot of code but I do not know enough yet to be able to help myself in this situation. Or perhaps I do and am just making it more complicated than it really is, I don't know. Quote Link to comment https://forums.phpfreaks.com/topic/246063-can-you-explain-this-code/#findComment-1263703 Share on other sites More sharing options...
xyph Posted August 30, 2011 Share Posted August 30, 2011 It attempts to drop a MySQL table. Then it tries to create a table Then it calls some unknown function which I can only guess loads the contents of an external page into a string, in a loop. Then it uses a bunch more unknown functions which I guess extracts information, in a loop. This includes some logic on how to break out of the current looping structure it's in. It then checks if it has extracted any info. If it has, dump that into the created table. Then it echo's how many times the first loop executed, and flushes the output buffer. It then echo's a bunch of information based on the amount of times the previous loop executed. I doubt this is going to help. The looping structure that function employs is pretty nasty. Also, the way it extracts data from a web page seems fragile. Quote Link to comment https://forums.phpfreaks.com/topic/246063-can-you-explain-this-code/#findComment-1263705 Share on other sites More sharing options...
dodgeitorelse Posted August 30, 2011 Author Share Posted August 30, 2011 ok thank you. what do you mean by fragile? easy to fault? Quote Link to comment https://forums.phpfreaks.com/topic/246063-can-you-explain-this-code/#findComment-1263707 Share on other sites More sharing options...
xyph Posted August 30, 2011 Share Posted August 30, 2011 Easily broken Quote Link to comment https://forums.phpfreaks.com/topic/246063-can-you-explain-this-code/#findComment-1263722 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.