Jump to content

Strange results from an array


mrt003003

Recommended Posts

Hi there i have an array that catches all appropriate records from my database and selects a random row which is then outputted as a hyperlink.

 

$colname_Class3 = "3";
$colname_Ships3 = "-1";
if (isset($_SESSION['MM_Username'])) {
  $colname_Ships3 = (get_magic_quotes_gpc()) ? $_SESSION['MM_Username'] : addslashes($_SESSION['MM_Username']);
}
mysql_select_db($database_swb, $swb);
$query_Ships3 = sprintf("SELECT * FROM ships WHERE PlayerName = %s AND Class=%s AND Template='1'", GetSQLValueString($colname_Ships3, "text"),
GetSQLValueString($colname_Class3, "int"));
$Ships3 = mysql_query($query_Ships3, $swb) or die(mysql_error());
$totalRows_Ships3 = mysql_num_rows($Ships3);

$count = 0;
while ($row = mysql_fetch_assoc($Ships3)){
$array[$count]['ShipID'] = $row['ShipID'];
$array[$count]['ShipName'] = $row['ShipName'];
$array[$count]['Class'] = $row['Class'];
$count++;
}

$total3 = count($array3);
$random3 = rand(0,$total3 - 1);
$random_ship3 = $array3[$random3];
$ShipID3 = $random_ship3['ShipID'];
$url3 = '<a href="add_ship.php?recordID='.$ShipID3.'">Test3</a>';
echo '<span style="color: #009900">';
echo $url3;}

 

This all worked fine until i added another similar query:

$colname_Class4 = "4";
$colname_Ships4 = "-1";
if (isset($_SESSION['MM_Username'])) {
  $colname_Ships4 = (get_magic_quotes_gpc()) ? $_SESSION['MM_Username'] : addslashes($_SESSION['MM_Username']);
}
mysql_select_db($database_swb, $swb);
$query_Ships4 = sprintf("SELECT * FROM ships WHERE PlayerName = %s AND Class=%s AND Template='1'", GetSQLValueString($colname_Ships4, "text"),
GetSQLValueString($colname_Class4, "int"));
$Ships4 = mysql_query($query_Ships4, $swb) or die(mysql_error());
$totalRows_Ships4 = mysql_num_rows($Ships4);

$count = 0;
while ($row = mysql_fetch_assoc($Ships4)){
$array[$count]['ShipID'] = $row['ShipID'];
$array[$count]['ShipName'] = $row['ShipName'];
$array[$count]['Class'] = $row['Class'];
$count++;
}

$total4 = count($array4);
$random4 = rand(0,$total4 - 1);
$random_ship4 = $array4[$random4];
$ShipID4 = $random_ship4['ShipID'];
$url4 = '<a href="add_ship.php?recordID='.$ShipID4.'">Test4</a>';
echo $url4;}

 

On first look it seems fine, however once the page is refreshed the parsed recordID's both become the same number,

e.g: Test3 recordID 8 Test4 recordID 24

then after refreshed: Test3 recordID 24 Test4 recordID 24

 

Is there anything wrong with my code??

 

Thanks :)

Link to comment
https://forums.phpfreaks.com/topic/235932-strange-results-from-an-array/
Share on other sites

Hi there i have an array that catches all appropriate records from my database and selects a random row which is then outputted as a hyperlink.

If all you're doing is wanting to displaying a random row. Then there is no need to get all the rows from mysql and then choose a random row with PHP. MySQL can select a random row, example query.

SELECT * FROM ships WHERE PlayerName = %s AND Class=%s AND Template='1' ORDER BY rand() LIMIT 1

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.