select from one table insert to another
Posted: Mon May 30, 2011 3:20 pm
I am making my matchup.php
I am trying to take 2 names from one table and insert them to a new table in one row
like this now I am unsure what to do I know the foreach is probly incorrect
I am trying to take 2 names from one table and insert them to a new table in one row
like this now I am unsure what to do I know the foreach is probly incorrect
<?php $localhost = "localhost"; $username = "test"; $password = "test"; $database = "test"; mysql_connect($localhost,$username,$password); mysql_select_db($database) or die( "Unable to select database"); if (!mysql_select_db($database)) { echo "Unable to select db: " . mysql_error(); exit; } $sql = "SELECT * FROM `users` ORDER BY RAND() limit 2"; $result = mysql_query($sql); if (!$result) { echo "Could not successfully run query ($sql) from DB: " . mysql_error(); exit; } if (mysql_num_rows($result) == 0) { echo "No rows found, nothing to print so am exiting"; exit; } while ($row = mysql_fetch_assoc($result)) { extract($row); //echo $row["id"]; //echo $row["username"]; echo $username.'</br>'; foreach($username as $username_1){ mysql_query("INSERT INTO `matchups` ('tournament_id','username_1','username_2' ) VALUES ('',$username_1','$username_2')"); } } mysql_free_result($result); ?>