wintallo Posted November 18, 2006 Share Posted November 18, 2006 Hey, I was wondering how I could select the first 20 rows of a MySQL table and insert those same rows into a new table. This is my code for ordering a table by score descending and selecting the first twenty rows and then printing them out on the page.[code]<?phpinclude 'db.php';if(!isset($_GET['page'])){ $page = 1;} else { $page = $_GET['page'];}$max_results = 20;$from = (($page * $max_results) - $max_results); $sql = mysql_query("SELECT * FROM tetris ORDER BY score DESC LIMIT $from, $max_results");while($row = mysql_fetch_array($sql)){ echo "<tr><td>"; echo $row['name']; echo "</td><td>"; echo $row['score']; echo "</td><td>"; echo $row['date']; echo "</td></tr>";}?>[/code]What could I do to this code to copy the selected rows into a table called tetris_hs.Thanks for reading.Joelwintallo.com Link to comment https://forums.phpfreaks.com/topic/27687-solved-mysql-questions/ Share on other sites More sharing options...
Adika Posted November 18, 2006 Share Posted November 18, 2006 Just do like this:[code]<?phpinclude 'db.php';if(!isset($_GET['page'])){ $page = 1;} else { $page = $_GET['page'];}$max_results = 20;$from = (($page * $max_results) - $max_results); $sql = mysql_query("SELECT * FROM tetris ORDER BY score DESC LIMIT $from, $max_results");while($row = mysql_fetch_array($sql)){ echo "<tr><td>"; echo $row['name']; echo "</td><td>"; echo $row['score']; echo "</td><td>"; echo $row['date']; echo "</td></tr>";// Add this code, which will insert every result into new database $sql1 = mysql_query("INSERT INTO tetris_hs(name,score,date) VALUES ('$row[name]','$row[score]','$row['date']')");}?>[/code] Link to comment https://forums.phpfreaks.com/topic/27687-solved-mysql-questions/#findComment-126635 Share on other sites More sharing options...
wintallo Posted November 18, 2006 Author Share Posted November 18, 2006 Hmmm... it looks like it should work, but an error comes up saying Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/wintallo/public_html/sandbox/tetris_highscores_score_d.php on line 53just so yah know, line 53 is [code]$sql1 = mysql_query("INSERT INTO tetris_hs(name,score,date) VALUES ('$row[name]','$row[score]','$row['date']')");[/code]Thanks for helping though!Joel wintallo.com Link to comment https://forums.phpfreaks.com/topic/27687-solved-mysql-questions/#findComment-126639 Share on other sites More sharing options...
Adika Posted November 18, 2006 Share Posted November 18, 2006 Hi. I made a little mistake in my code. Sorry for that. Here is the good code.[code]$sql1 = mysql_query("INSERT INTO tetris_hs(name,score,date) VALUES ('$row[name]','$row[score]','$row[date]')");[/code] Link to comment https://forums.phpfreaks.com/topic/27687-solved-mysql-questions/#findComment-126640 Share on other sites More sharing options...
printf Posted November 18, 2006 Share Posted November 18, 2006 Just SELECT INTO... (1 query)[code]$sql = "INSERT INTO tetris_hs (name, score, date) SELECT name, score, date FROM tetris ORDER BY score DESC LIMIT " . $from . ", " . $max_results;mysql_query ( $sql );[/code]printf Link to comment https://forums.phpfreaks.com/topic/27687-solved-mysql-questions/#findComment-126641 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.