PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
I am attempting to simplify the above code. I came up with the below, but it keep producing an internal server error. Is this due to a problem with the code, and if so, how can it be fixed.
$result = mysql_query("SELECT won FROM attack_log WHERE attacker_id = '$userid' ORDER BY battle_id DESC LIMIT 0, 10");
Does that help? ^^ The query. I am wishing to asign $attack1, $attack2 etc to different rows. It is NOT different fields from the same row., It IS different rows from the same field. Maybe i could still do i differently but i dont really see how. I would know if it was different fields from the same row but it's not that.
Yes that is part of the question - what code to use. Up to now i've just been using mysql_result($result, $i-1, 'won'); but it's always before been part of some loop, maybe even that is still inefficient. So what to you recommend?
Currently - my method works, and yours doesn't. However, if your method if more efficient then i would be interested to know how to do it - but at the moment, it just wont work
<?php
$attack = array();
$result = mysql_query("SELECT won FROM attack_log WHERE attacker_id = '$userid' ORDER BY battle_id DESC LIMIT 0, 10");
while($row = mysql_fetch_assoc($result)) { // this will loop through every row in $result
$attack[] = $row['won'];
}
echo $attack[0];
echo $attack[1];
echo $attack[2];
// .....
?>
<?php
$attack = array();
$result = mysql_query("SELECT won FROM attack_log WHERE attacker_id = '$userid' ORDER BY battle_id DESC LIMIT 0, 10");
while($row = mysql_fetch_assoc($result)) { // this will loop through every row in $result
$attack[] = $row['won'];
}
echo $attack[0];
echo $attack[1];
echo $attack[2];
// .....
?>
If you read the manual page on mysql_result() it says that for large data sets, mysql_* array functions are probably better. What you are doing in your code is running through a mysql_query result and, row by row telling the script for each row, pull information from the col 'won' and read that into a numbered var that begins $attack. This is inefficent. If you were to use mysql_fetch_array, you would be able to loop the result array and reference these row within the loop.
<?php
if (!$result = mysql_query($query))
{
error_out('The query did not work: ' . mysql_error()); // This should be a custom error handler
}
$attacks = array();
while ($attacks[] = mysql_fetch_array($result))
{
$attacks_count = $count($attacks);
}
// Now loop the attacks array
for ($i = 0; $i < $attacks_count; $i++)
{
echo '<p>This is row number ' . $i . ' and the value for won is ' . $attacks[$i]['won'] . '.</p>';
}
?>