Aravinthan Posted August 12, 2009 Author Share Posted August 12, 2009 Changed it, but same response... Code: <?php function sql_safe($value) { $value = trim($value); if(is_numeric($value)) return $value; else return "'$value'"; } // DONT FORGET TO CONNECT TO MYSQL! $conn = mysql_connect('localhost', 'liguehs_ara', '1992arayugi'); mysql_select_db('liguehs_league'); $player_table_name = 'players'; $check="SELECT * FROM $player_table_name"; $result=@mysql_query($check); if (!$result){ $make_table = 'CREATE TABLE IF NOT EXISTS `liguehs_league`.`players` (`id` INT( 11 ) NOT NULL AUTO_INCREMENT, `shooting` INT(5) NOT NULL, `playmaking` INT(5) NOT NULL, `stickhandling` INT(5) NOT NULL, `checking` INT(5) NOT NULL, `marking` INT(5) NOT NULL, `hitting` INT(5) NOT NULL, `skating` INT(5) NOT NULL, `endurance` INT(5) NOT NULL, `penalty` INT(5) NOT NULL, `faceoffs` INT(5) NOT NULL, `leadership` INT(5) NOT NULL, `strength` INT(5) NOT NULL, `potentiel` INT(5) NOT NULL, `consistency` INT(5) NOT NULL, `greed` INT(5) NOT NULL, `fighting` INT(5) NOT NULL, `click` INT(5) NOT NULL, `team` INT(5) NOT NULL, `main_position` INT(5) NOT NULL, `country` INT(5) NOT NULL, `birth_year` INT(5) NOT NULL, `birth_day` INT(5) NOT NULL, `birth_month` INT(5) NOT NULL, `salary` INT(11) NOT NULL, `contract_lenght` INT(5) NOT NULL, `draft_year` INT(5) NOT NULL, `draft_round` INT(5) NOT NULL, `drafted_by` INT(5) NOT NULL, `rights` INT(5) NOT NULL, `week_goals` INT(5) NOT NULL, `week_assists` INT(5) NOT NULL, `month_goals` INT(5) NOT NULL, `month_assists` INT(5) NOT NULL, `month_gwg` INT(5) NOT NULL, `record_goals` INT(5) NOT NULL, `record_assists` INT(5) NOT NULL, `record_points` INT(5) NOT NULL, `no_trade_switch` INT(5) NOT NULL, `two-way_switch` INT(5) NOT NULL, `player-team_option` INT(5) NOT NULL, `status` INT(5) NOT NULL, `rookie` INT(5) NOT NULL, `rconsidering_offer_data` INT(11) NOT NULL, `team_offering` INT(5) NOT NULL, `amount_time_spent_considering` INT(5) NOT NULL, `injury` INT(5) NOT NULL, `line8` INT(5) NOT NULL, `line9` INT(5) NOT NULL, `line10` INT(5) NOT NULL, `goal_streak` INT(5) NOT NULL, `point_streak` INT(5) NOT NULL, `total_gp` INT(5) NOT NULL, `point_streak` INT(5) NOT NULL, `suspended_game` INT(5) NOT NULL, `training` INT(5) NOT NULL, `weight` INT(5) NOT NULL, `height` INT(5) NOT NULL, `status_in_organization` INT(5) NOT NULL, `best_streak_games` INT(5) NOT NULL, `best_streak_gwg` INT(5) NOT NULL, `best_streak_assists` INT(5) NOT NULL, `best_streak_points` INT(5) NOT NULL, `best_streak_goals` INT(5) NOT NULL, `line13` INT(5) NOT NULL, `name` VARCHAR(100) NOT NULL, `line15` INT(5) NOT NULL, `drafted` VARCHAR(100) NOT NULL, `line17` INT(5) NOT NULL, `line18` INT(5) NOT NULL, `line19` INT(5) NOT NULL, `attitude` INT(5) NOT NULL, `alternate_position` INT(5) NOT NULL, `nhl_rights` INT(5) NOT NULL, `injury_prone` INT(5) NOT NULL, `overral_draft INT(5) NOT NULL) ENGINE = MyISAM'; } $players_data_file = file('players.txt'); // remove first line from file, this is line holds the number of players $total_players = array_shift($players_data_file); // DO NOT CHANGE THIS. THIS IS SET UP TO MATCH THE DATA IN THE DATA.TXT FILE $fields = array( array('shooting', 'playmaking', 'stickhandling', 'checking', 'marking', 'hitting', 'skating', 'endurance', 'penalty', 'faceoffs'), array('leadership', 'strength', 'potentiel', 'consistency', 'greed', 'fighting', 'click', 'team', 'main_position', 'country', 'handed'), array('birth_year', 'birth_day', 'birth_month', 'salary', 'contract_lenght', 'draft_year', 'draft_round', 'drafted_by', 'rights'), array('week_goals', 'week_assists', 'week_gwg', 'month_goals'), array('month_assists', 'month_gwg'), array('record_goals', 'record_assists', 'record_points', 'no_trade_switch', 'two-way_switch', 'player-team_option'), array('status', 'rookie', 'considering_offer_data', 'team_offering', 'amount_time_spent_considering', 'injury'), array('line8'), array('line9'), array('line10'), array('goal_streak', 'point_streak', 'total_gp', 'suspended_game', 'training', 'weight', 'height', 'status_in_organization'), array('best_streak_games', 'best_streak_gwg', 'best_streak_assists', 'best_streak_points', 'best_streak_goals'), array('line13'), array('name'), array('line15'), array('drafeted'), array('line17'), array('line18'), array('line19'), array('attitude', 'alternate_position', 'nhl_rights', 'injury_prone', 'overral_draft') ); // split every 20 lines into a new array $all_players_data = array_chunk($players_data_file, 20); // loop through each block of 20 lines foreach($all_players_data as $player_data) { // check to see if the player already exist $player_name = sql_safe($player_data[13]); $user_exists_sql = "SELECT `name` FROM `$player_table_name` WHERE `name`=$player_name "; $result = mysql_query($user_exists_sql); // if the user does not exist, use an INSERT STATEMENT // we'll add the user into the database if(mysql_num_rows($result) == '0') { $sql = "INSERT INTO `$player_table_name` SET "; $update_user = false; } // user does exist so we'll use an UPDATE statement // update the existing user else { $sql = "UPDATE `$player_table_name` "; $update_user = true; } // here we're looping through each line foreach($player_data as $line_key => $line_data) { // if we're not on line 8, 9, 10, 13, 14 or 15 // turn line data into an array if(!in_array($line_key, array(7, 8, 9, 12, 13, 14))) { $field_data = explode(' ', $line_data); // match up the data with the field array // this is where our query is generated foreach($field_data as $field_key => $field_value) { if(isset($fields[$line_key][$field_key])) $sql .= "`" . $fields[$line_key][$field_key] . "`=" . sql_safe($field_value) . ', '; } $sql .= "\n"; } // add the whole line into the query else { // if we're updating the user skip line 14 if( $update_user && $line_key == 13) continue; $sql .= "`" . $fields[$line_key][0] . "`=" . sql_safe($line_data) . ", \n"; } } // remove the last three characters from $sql $sql = substr($sql, 0, -3); // add the WHERE clause only if we are updating the user if($update_user) $sql .= " WHERE name=$player_name"; echo '<pre>' . print_r($sql, true) . '</pre>'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/169513-reading-a-text-file/page/3/#findComment-896682 Share on other sites More sharing options...
wildteen88 Posted August 12, 2009 Share Posted August 12, 2009 Okay lets see what the error is Change this line $result = mysql_query($user_exists_sql); To $result = mysql_query($user_exists_sql) or die(mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/169513-reading-a-text-file/page/3/#findComment-896683 Share on other sites More sharing options...
Aravinthan Posted August 12, 2009 Author Share Posted August 12, 2009 Error: Table 'liguehs_league.players' doesn't exist So I guess there is an error with my coding no? Quote Link to comment https://forums.phpfreaks.com/topic/169513-reading-a-text-file/page/3/#findComment-896686 Share on other sites More sharing options...
Aravinthan Posted August 12, 2009 Author Share Posted August 12, 2009 OK I fell really dumb.... Forgot to execute my code lol.... Added this: $create_table = mysql_query($make_table) or die(mysql_error()); And it gives this error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 77 Line 77: `total_gp` INT(5) NOT NULL, Cant see any errors there... Quote Link to comment https://forums.phpfreaks.com/topic/169513-reading-a-text-file/page/3/#findComment-896690 Share on other sites More sharing options...
Aravinthan Posted August 12, 2009 Author Share Posted August 12, 2009 OK so I figured out the mistakes... Thank god!! lol Ok so now the output is: http://liguedhockeysimule.x10hosting.com/converter/test.php But the data is not getting inserted.... Why is that? Quote Link to comment https://forums.phpfreaks.com/topic/169513-reading-a-text-file/page/3/#findComment-896700 Share on other sites More sharing options...
wildteen88 Posted August 12, 2009 Share Posted August 12, 2009 Okay I have tested the code fully now and there are inconsistencies with the table names. The data in the player.txt file does not match up with the table names you have supplied. The way I am matching up the data in the player.txt file with the correct field name is by the example query you provided earlier Update( SET name = 'Milan Hnilicka', shooting = '63', playmaking = '61', stickhandling = '54', checking = '61', marking = '63', hitting = '60', skating = '63', endurance = '70', penalty = '66', faceoffs = '63', leadership = '63', strength = '71', potentiel = '68', consistency = '61', greed = '115',fighting = '42', click = '10', team='0', main_position='1', country='4', handed='1', birth_year='1973', birth_day='24', birth_month='6', salary='950000', contract_lenght='0', draft_year='1991', draft_round='4', drafted_by='17', rights='98', week_goals=0', week_assists='0', week_gwg='0', month_goals='0', month_assists='0', month_gwg='0', record_goals='179000', record_assists='3000', record_points='908', no_trade_switch='0', two-way_switch='0', player-team_option='0', status='1', rookie='0', considering_offer_data='0', team_offering='0', amount_time_spent_considering='0', injury='0', line8='0 0 0 0 0 0 0 0 0 0', line9='0 0 0 0 0 0 0 0 0 0', line10='0 0 0 0 0 0 0 0 0 0', goal_streak='0', point_streak='0', total_gp='0', suspended_game='0', training='0', weight='194', height='7', status_in_organization='3', best_streak_games='0', best_streak_gwg='0', best_streak_assists='0', best_streak_points='0', best_streak_goals='0', line13='-', line15='/n', drafted='drafted='drafted', line17='150080070070075095144103068062125080125', line18='1.16 (24.3.2002)', line19='1.16 (24.3.2002)', attitude='0', alternate_position='0', nhl_rights='98', injury_prone='65', overral_draft='70' Where name='$name'); Can you confirm those are the correct field names? The way way the $fields array is constructed is by how the data is layed out in player.txt Quote Link to comment https://forums.phpfreaks.com/topic/169513-reading-a-text-file/page/3/#findComment-896701 Share on other sites More sharing options...
Aravinthan Posted August 12, 2009 Author Share Posted August 12, 2009 You are right, there is 2 Things that are missing. Line 4, it is supossed to be: week_games, week_goals, week_assists, week_points Forgot the week_games... so sorry. And line 5: month_games, month_goals, month_assists, month_points Again forgot a colum games.... I am so sorry, and I also forgot to mention this: Is it possible to add a colum age? After, overral_draft(last data), where it takes a date in another table and it subtracts it to give is age? OA = (shooting + playmaking + stickhandling + checking + hitting + marking + skating + endurance + penalty + faceoff + leadership + strength + potentiel + consistency + gree) / 15 OFF = (shooting + playmaking + stickhandling) / 3 DEF = DEF = (checking + marking + hitting) / 3 All 4 after the other ones.... Thanks for your help, Ara And there is 3 other stuffs: Quote Link to comment https://forums.phpfreaks.com/topic/169513-reading-a-text-file/page/3/#findComment-896712 Share on other sites More sharing options...
wildteen88 Posted August 12, 2009 Share Posted August 12, 2009 To clarify the parts highlighted in red 63 61 54 61 63 60 63 70 66 63 63 71 68 61 115 42 10 0 1 4 1 1973 24 6 950000 0 1991 4 17 98 0 0 0 0 0 0 0 0 179000 3000 908 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 4 194 7 3 0 0 0 0 0 - Milan Hnilicka drafted 150080070070075095144103068062125080125 1.16 (24.3.2002) 1.16 (24.3.2002) 0 0 98 65 70 Do not match up to a field in the database. Is this correct? All the other bits are inserted into the database fine. Quote Link to comment https://forums.phpfreaks.com/topic/169513-reading-a-text-file/page/3/#findComment-896714 Share on other sites More sharing options...
Aravinthan Posted August 12, 2009 Author Share Posted August 12, 2009 To clarify the parts highlighted in red 63 61 54 61 63 60 63 70 66 63 63 71 68 61 115 42 10 0 1 4 1 1973 24 6 950000 0 1991 4 17 98 0 0 0 0 0 0 0 0 179000 3000 908 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 4 194 7 3 0 0 0 0 0 - Milan Hnilicka drafted 150080070070075095144103068062125080125 1.16 (24.3.2002) 1.16 (24.3.2002) 0 0 98 65 70 Do not match up to a field in the database. Is this correct? All the other bits are inserted into the database fine. For the 98, It is supossed to be under colum rights, and I think thats ok. But the 3 others, should be under: month_ goals, month_assists, month_gwg Iam guessing, thats because there is no week_games and month_games Quote Link to comment https://forums.phpfreaks.com/topic/169513-reading-a-text-file/page/3/#findComment-896719 Share on other sites More sharing options...
wildteen88 Posted August 12, 2009 Share Posted August 12, 2009 Oops marked the wrong bit of data. Its the last bit of data on the second line. What is the field name for that? 63 61 54 61 63 60 63 70 66 63 63 71 68 61 115 42 10 0 1 4 1 ... Quote Link to comment https://forums.phpfreaks.com/topic/169513-reading-a-text-file/page/3/#findComment-896721 Share on other sites More sharing options...
Aravinthan Posted August 12, 2009 Author Share Posted August 12, 2009 Handed Quote Link to comment https://forums.phpfreaks.com/topic/169513-reading-a-text-file/page/3/#findComment-896726 Share on other sites More sharing options...
wildteen88 Posted August 12, 2009 Share Posted August 12, 2009 EDIT Before running the new code delete your players table first. Otherwise the script wont run properly. Yep I can confirm the script is now fully functional, here is the fixed code <?php function sql_safe($value) { $value = trim($value); if(is_numeric($value)) return $value; else return "'$value'"; } // DONT FORGET TO CONNECT TO MYSQL! $conn = mysql_connect('localhost', 'liguehs_ara', '1992arayugi'); mysql_select_db('liguehs_league'); $player_table_name = 'players'; $make_table = 'CREATE TABLE IF NOT EXISTS `'.$player_table_name.'` (`id` INT( 11 ) NOT NULL AUTO_INCREMENT PRIMARY KEY, `shooting` INT(5) NOT NULL, `playmaking` INT(5) NOT NULL, `stickhandling` INT(5) NOT NULL, `checking` INT(5) NOT NULL, `marking` INT(5) NOT NULL, `hitting` INT(5) NOT NULL, `skating` INT(5) NOT NULL, `endurance` INT(5) NOT NULL, `penalty` INT(5) NOT NULL, `faceoffs` INT(5) NOT NULL, `leadership` INT(5) NOT NULL, `strength` INT(5) NOT NULL, `potentiel` INT(5) NOT NULL, `consistency` INT(5) NOT NULL, `greed` INT(5) NOT NULL, `fighting` INT(5) NOT NULL, `click` INT(5) NOT NULL, `team` INT(5) NOT NULL, `main_position` INT(5) NOT NULL, `country` INT(5) NOT NULL, `handed` INT(5) NOT NULL, `birth_year` INT(5) NOT NULL, `birth_day` INT(5) NOT NULL, `birth_month` INT(5) NOT NULL, `salary` INT(11) NOT NULL, `contract_lenght` INT(5) NOT NULL, `draft_year` INT(5) NOT NULL, `draft_round` INT(5) NOT NULL, `drafted_by` INT(5) NOT NULL, `rights` INT(5) NOT NULL, `week_games` INT(5) NOT NULL, `week_goals` INT(5) NOT NULL, `week_assists` INT(5) NOT NULL, `week_points` INT(5) NOT NULL, `month_games` INT(5) NOT NULL, `month_goals` INT(5) NOT NULL, `month_assists` INT(5) NOT NULL, `month_points` INT(5) NOT NULL, `record_goals` INT(5) NOT NULL, `record_assists` INT(5) NOT NULL, `record_points` INT(5) NOT NULL, `no_trade_switch` INT(5) NOT NULL, `two-way_switch` INT(5) NOT NULL, `player-team_option` INT(5) NOT NULL, `status` INT(5) NOT NULL, `rookie` INT(5) NOT NULL, `considering_offer_data` INT(11) NOT NULL, `team_offering` INT(5) NOT NULL, `amount_time_spent_considering` INT(5) NOT NULL, `injury` INT(5) NOT NULL, `line8` VARCHAR(50) NOT NULL, `line9` VARCHAR(50) NOT NULL, `line10` VARCHAR(50) NOT NULL, `goal_streak` INT(5) NOT NULL, `point_streak` INT(5) NOT NULL, `total_gp` INT(5) NOT NULL, `suspended_game` INT(5) NOT NULL, `training` INT(5) NOT NULL, `weight` INT(5) NOT NULL, `height` INT(5) NOT NULL, `status_in_organization` INT(5) NOT NULL, `best_streak_games` INT(5) NOT NULL, `best_streak_gwg` INT(5) NOT NULL, `best_streak_assists` INT(5) NOT NULL, `best_streak_points` INT(5) NOT NULL, `best_streak_goals` INT(5) NOT NULL, `line13` VARCHAR(50) NOT NULL, `name` VARCHAR(100) NOT NULL, `line15` VARCHAR(50) NOT NULL, `drafted` VARCHAR(100) NOT NULL, `line17` VARCHAR(50) NOT NULL, `line18` VARCHAR(50) NOT NULL, `line19` VARCHAR(50) NOT NULL, `attitude` INT(5) NOT NULL, `alternate_position` INT(5) NOT NULL, `nhl_rights` INT(5) NOT NULL, `injury_prone` INT(5) NOT NULL, `overral_draft` INT(5) NOT NULL ) ENGINE = MYISAM ;'; mysql_query($make_table) or die('Cannot create table ' . $player_table_name . '<br />'.mysql_error()); $players_data_file = file('players.txt'); // remove first line from file, this is line holds the number of players $total_players = array_shift($players_data_file); // DO NOT CHANGE THIS. THIS IS SET UP TO MATCH THE DATA IN THE DATA.TXT FILE $fields = array( array('shooting', 'playmaking', 'stickhandling', 'checking', 'marking', 'hitting', 'skating', 'endurance', 'penalty', 'faceoffs'), array('leadership', 'strength', 'potentiel', 'consistency', 'greed', 'fighting', 'click', 'team', 'main_position', 'country', 'handed'), array('birth_year', 'birth_day', 'birth_month', 'salary', 'contract_lenght', 'draft_year', 'draft_round', 'drafted_by', 'rights'), array('week_games', 'week_goals', 'week_assists', 'week_points'), array('month_games', 'month_goals', 'month_assists', 'month_points'), array('record_goals', 'record_assists', 'record_points', 'no_trade_switch', 'two-way_switch', 'player-team_option'), array('status', 'rookie', 'considering_offer_data', 'team_offering', 'amount_time_spent_considering', 'injury'), array('line8'), array('line9'), array('line10'), array('goal_streak', 'point_streak', 'total_gp', 'suspended_game', 'training', 'weight', 'height', 'status_in_organization'), array('best_streak_games', 'best_streak_gwg', 'best_streak_assists', 'best_streak_points', 'best_streak_goals'), array('line13'), array('name'), array('line15'), array('drafted'), array('line17'), array('line18'), array('line19'), array('attitude', 'alternate_position', 'nhl_rights', 'injury_prone', 'overral_draft') ); // split every 20 lines into a new array $all_players_data = array_chunk($players_data_file, 20); // loop through each block of 20 lines foreach($all_players_data as $player_data) { // check to see if the player already exist $player_name = sql_safe($player_data[13]); $user_exists_sql = "SELECT `name` FROM `$player_table_name` WHERE `name`=$player_name "; $result = mysql_query($user_exists_sql) or die('Error checking user!<br />'.mysql_error()); // if the user does not exist, use an INSERT STATEMENT // we'll add the user into the database if(mysql_num_rows($result) == '0') { $sql = "INSERT INTO `$player_table_name` SET "; $update_user = false; } // user does exist so we'll use an UPDATE statement // update the existing user else { $sql = "UPDATE `$player_table_name` SET "; $update_user = true; } // here we're looping through each line foreach($player_data as $line_key => $line_data) { // if we're not on line 8, 9, 10, 13, 14 or 15 // turn line data into an array if(!in_array($line_key, array(7, 8, 9, 12, 13, 14))) { $field_data = explode(' ', $line_data); // match up the data with the field array // this is where our query is generated foreach($field_data as $field_key => $field_value) { if(isset($fields[$line_key][$field_key])) $sql .= "`" . $fields[$line_key][$field_key] . "`=" . sql_safe($field_value) . ', '; } $sql .= "\n"; } // add the whole line into the query else { // if we're updating the user skip line 14 if( $update_user && $line_key == 13) continue; $sql .= "`" . $fields[$line_key][0] . "`=" . sql_safe($line_data) . ", \n"; } } // remove the last three characters from $sql $sql = substr($sql, 0, -3); // add the WHERE clause only if we are updating the user if($update_user) $sql .= " WHERE name=$player_name"; echo '<pre>' . print_r($sql, true) . '</pre>'; mysql_query($sql) or die('Cannot inset/update table!'.mysql_error()); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/169513-reading-a-text-file/page/3/#findComment-896731 Share on other sites More sharing options...
Aravinthan Posted August 13, 2009 Author Share Posted August 13, 2009 Hi wow, thanks but can you tell where and how can I put these?: After, overral_draft(last data), where it takes a date in another table and it subtracts it to give is age? OA = (shooting + playmaking + stickhandling + checking + hitting + marking + skating + endurance + penalty + faceoff + leadership + strength + potentiel + consistency + gree) / 15 OFF = (shooting + playmaking + stickhandling) / 3 DEF = DEF = (checking + marking + hitting) / 3 Quote Link to comment https://forums.phpfreaks.com/topic/169513-reading-a-text-file/page/3/#findComment-896869 Share on other sites More sharing options...
Aravinthan Posted August 13, 2009 Author Share Posted August 13, 2009 UP Quote Link to comment https://forums.phpfreaks.com/topic/169513-reading-a-text-file/page/3/#findComment-897201 Share on other sites More sharing options...
Aravinthan Posted August 13, 2009 Author Share Posted August 13, 2009 Edit: Never mind it works now!!! Thanks so much! Quote Link to comment https://forums.phpfreaks.com/topic/169513-reading-a-text-file/page/3/#findComment-897229 Share on other sites More sharing options...
Aravinthan Posted August 13, 2009 Author Share Posted August 13, 2009 Ok, I got a little problem.... It just inserts the first 115.... not the rest.... How come? Thansk for your help, ARa Quote Link to comment https://forums.phpfreaks.com/topic/169513-reading-a-text-file/page/3/#findComment-897303 Share on other sites More sharing options...
wildteen88 Posted August 13, 2009 Share Posted August 13, 2009 Are you using the new code I supplied here? Do you get any errors? If you do post them here. Quote Link to comment https://forums.phpfreaks.com/topic/169513-reading-a-text-file/page/3/#findComment-897324 Share on other sites More sharing options...
Aravinthan Posted August 13, 2009 Author Share Posted August 13, 2009 You are right, there is an error: Error checking user! You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Aversa'' at line 1 EDIT: I checked teh players file and the name is: Jonathan D'Aversa So thats why.... I put addslashes() $player_name = addslashes(sql_safe($player_data[13])); But it shows me: Error checking user! You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '\'Milan Hnilicka\'' at line 1 Thanks for your help, Ara Quote Link to comment https://forums.phpfreaks.com/topic/169513-reading-a-text-file/page/3/#findComment-897471 Share on other sites More sharing options...
MadTechie Posted August 13, 2009 Share Posted August 13, 2009 put the sql_safe in the sql_safe function change function sql_safe($value) { $value = trim($value); if(is_numeric($value)) return $value; else return "'$value'"; } to function sql_safe($value) { $value = trim($value); if(is_numeric($value)) return $value; else return "'".addslashes($value)."'"; } and remove the one you added back to $player_name = sql_safe($player_data[13]); OR JUST do $player_name = sql_safe(addslashes($player_data[13])); Quote Link to comment https://forums.phpfreaks.com/topic/169513-reading-a-text-file/page/3/#findComment-897513 Share on other sites More sharing options...
Aravinthan Posted August 13, 2009 Author Share Posted August 13, 2009 Oh yeah of course!!!! Thanks so much, but like usual another problem -.- Cannot inset/update table!MySQL server has gone away Quote Link to comment https://forums.phpfreaks.com/topic/169513-reading-a-text-file/page/3/#findComment-897522 Share on other sites More sharing options...
Aravinthan Posted August 13, 2009 Author Share Posted August 13, 2009 So I contacted my host, and they told me I should try splitting it... 500 datas and then another 500.... Quote Link to comment https://forums.phpfreaks.com/topic/169513-reading-a-text-file/page/3/#findComment-897542 Share on other sites More sharing options...
MadTechie Posted August 13, 2009 Share Posted August 13, 2009 you could create another loop to break it into smaller chunks here's a basic example <?php // split every 20 lines into a new array $all_players_data = array_chunk($players_data_file, 20); //Calculate Number of chuncks $MaxChunk = 500; $Chunks = ceil(count($all_players_data / $MaxChunk)); //Break into chunks $chunk_all_players_data = array_chunk($all_players_data, $Chunks, true); //Loop through each chunk foreach($chunk_all_players_data as $chunk_players_data) { // loop through each block of 20 lines foreach($chunk_players_data as $player_data) //UPDATED { //.....SNIP mysql_query($sql) or die('Cannot inset/update table!'.mysql_error()); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/169513-reading-a-text-file/page/3/#findComment-897606 Share on other sites More sharing options...
Aravinthan Posted August 13, 2009 Author Share Posted August 13, 2009 You lost me there... Lol OK so this is what it gives me: // split every 20 lines into a new array $all_players_data = array_chunk($players_data_file, 20); //Calculate Number of chuncks $MaxChunk = 500; $Chunks = ceil(count($all_players_data / $MaxChunk)); //Break into chunks $chunk_all_players_data = array_chunk($all_players_data, $Chunks, true); //Loop through each chunk foreach($chunk_all_players_data as $chunk_players_data) { // loop through each block of 20 lines foreach($chunk_players_data as $player_data) //UPDATED { // check to see if the player already exist $player_name = sql_safe($player_data[13]); $user_exists_sql = "SELECT `name` FROM `$player_table_name` WHERE `name`=$player_name "; $result = mysql_query($user_exists_sql) or die('Error checking user!<br />'.mysql_error()); // if the user does not exist, use an INSERT STATEMENT // we'll add the user into the database if(mysql_num_rows($result) == '0') { $sql = "INSERT INTO `$player_table_name` SET "; $update_user = false; } // user does exist so we'll use an UPDATE statement // update the existing user else { $sql = "UPDATE `$player_table_name` SET "; $update_user = true; } // here we're looping through each line foreach($player_data as $line_key => $line_data) { // if we're not on line 8, 9, 10, 13, 14 or 15 // turn line data into an array if(!in_array($line_key, array(7, 8, 9, 12, 13, 14))) { $field_data = explode(' ', $line_data); // match up the data with the field array // this is where our query is generated foreach($field_data as $field_key => $field_value) { if(isset($fields[$line_key][$field_key])) $sql .= "`" . $fields[$line_key][$field_key] . "`=" . sql_safe($field_value) . ', '; } $sql .= "\n"; } // add the whole line into the query else { // if we're updating the user skip line 14 if( $update_user && $line_key == 13) continue; $sql .= "`" . $fields[$line_key][0] . "`=" . sql_safe($line_data) . ", \n"; } } // remove the last three characters from $sql $sql = substr($sql, 0, -3); // add the WHERE clause only if we are updating the user if($update_user) $sql .= " WHERE name=$player_name"; echo '<pre>' . print_r($sql, true) . '</pre>'; mysql_query($sql) or die('Cannot inset/update table!'.mysql_error()); } } Unfortunatley, I get this error: Fatal error: Unsupported operand types in /home/liguehs/public_html/converter/test.php on line 141 Quote Link to comment https://forums.phpfreaks.com/topic/169513-reading-a-text-file/page/3/#findComment-897629 Share on other sites More sharing options...
Aravinthan Posted August 13, 2009 Author Share Posted August 13, 2009 Forgot to mention. Line 141 is: $Chunks = ceil(count($all_players_data / $MaxChunk)); Quote Link to comment https://forums.phpfreaks.com/topic/169513-reading-a-text-file/page/3/#findComment-897630 Share on other sites More sharing options...
Aravinthan Posted August 13, 2009 Author Share Posted August 13, 2009 Ok so I change: $all_players_data To 3500, It worked, but it doesnt insert all, there still some missing and I get the MYSQL server gone error.... Now I am playing with the 3500 and MaxChunk number without any result... Quote Link to comment https://forums.phpfreaks.com/topic/169513-reading-a-text-file/page/3/#findComment-897665 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.