Jump to content

[SOLVED] MySQL Questions


wintallo

Recommended Posts

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]
<?php

include '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.

Joel
wintallo.com
Link to comment
https://forums.phpfreaks.com/topic/27687-solved-mysql-questions/
Share on other sites

Just do like this:

[code]
<?php

include '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]
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 53

just 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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.