erdem Posted February 3, 2012 Share Posted February 3, 2012 Hi, I'm trying to insert some data to mysql database. but I don't know what the MySQL structure should be and how to write it. this is html output. thanks so much. Player name - Race Color - Team - Average APM - Winner? Mike Zerg Red Team 1 156 1 (win) WinD Zerg Blue Team 2 163 0 (lose) and this is the code. $obsString = ""; $obsCount = 0; echo "<table border=\"1\"><tr><th>Player name</th><th>Race</th><th>Color</th><th>Team</th><th>Average APM<br />(experimental)</th><th>Winner?</th></tr>\n"; foreach($players as $value) { if ($value['isObs']) { if ($obsString == "") $obsString = $value['name']; else $obsString .= ', '.$value['name']; $obsCount++; continue; } if ($b->isWinnerKnown()) $wincolor = (isset($value['won']) && $value['won'] == 1)?0x00FF00:0xFF0000; else $wincolor = 0xFFFFFF; if ($value['isComp'] && $b->getTeamSize() !== null) $difficultyString = sprintf(" (%s)",SC2Replay::$difficultyLevels[$value['difficulty']]); else $difficultyString = ""; echo sprintf("<tr><td>%s</td><td>%s</td><td><font color=\"#%s\">%s</font></td><td>%s</td><td style=\"text-align: center\">%d</td><td style=\"background-color: #%06X; text-align: center\">%d</td></tr>\n", $value['name'].$difficultyString, $value['race'], $value['color'], $value['sColor'], ($value['team'] > 0)?"Team ".$value['team']:"-", ($value['team'] > 0)?(round($value['apmtotal'] / ($b->getGameLength() / 60))):0, ((isset($value['won']))?$wincolor:0xFFFFFF), (isset($value['won']))?$value['won']($value['team'] > 0)?"Unknown":"-") ); } echo "</table><br />"; if ($obsCount > 0) { echo "Observers ($obsCount): $obsString<br />\n"; } Quote Link to comment https://forums.phpfreaks.com/topic/256330-how-can-i-inster-database/ Share on other sites More sharing options...
spiderwell Posted February 3, 2012 Share Posted February 3, 2012 what have you got so far, there is not anything in your code that relates to a database. is the array $players filled from a db query? Quote Link to comment https://forums.phpfreaks.com/topic/256330-how-can-i-inster-database/#findComment-1314025 Share on other sites More sharing options...
erdem Posted February 3, 2012 Author Share Posted February 3, 2012 no it comes from a file. Star Craft 2 file. it goes to an array. the replay parsing. http://code.google.com/p/phpsc2replay/wiki/Parseables Quote Link to comment https://forums.phpfreaks.com/topic/256330-how-can-i-inster-database/#findComment-1314034 Share on other sites More sharing options...
digibucc Posted February 3, 2012 Share Posted February 3, 2012 here's what i did: $entry would be your array, with all of your info. this foreach will split that into key value pairs, and then format them as an SQL query, and finally it will insert. <?php foreach ($entry as $key=>$value){ if ($value != '' && $value != 'Submit'){ $cols .= mysql_real_escape_string($key). ', '; $vals .= '\''. mysql_real_escape_string($value). '\', '; } } $columns = substr($cols,0,-2); // trim trailing "'," , $values = substr($vals,0,-2); $sql="INSERT INTO table ( $columns )VALUES ( $values )"; i'm sure there are other ways to handle the trailing "',", but this works for me Quote Link to comment https://forums.phpfreaks.com/topic/256330-how-can-i-inster-database/#findComment-1314035 Share on other sites More sharing options...
spiderwell Posted February 3, 2012 Share Posted February 3, 2012 *** EDIT post above seems to cover it better than i did *** first thing would be to create a database, and then in that database you would need a table with columns with names like the ones in the array. A unique identifier which is usually called id or perhaps in this case call it player_id, have it auto increment and as a primary key. then have other columns, use the <th> values as a guide to what you need. once you have this table ready you can go about setting up a database connection and sql statement to insert the data into the table on the database Quote Link to comment https://forums.phpfreaks.com/topic/256330-how-can-i-inster-database/#findComment-1314038 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.