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
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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.