Note that there are some explanatory texts on larger screens.

plurals
  1. POGetting different results when using a MySQL query array, compared to a PHP array
    primarykey
    data
    text
    <p>I'm writing a script that works out the mode, median and mean of a set of data. I have ten records which are stored in a MySQL database and in a PHP array. When the script is finished, the script will only use the data stored in the database and not the PHP array, the PHP array is there to test the code.</p> <p>What I have found though is that the results are different when using the PHP array compared to the MySQL array. In the PHP array, I have the following data:</p> <pre><code>$arr = array(60, 70, 71, 76, 144, 151, 197, 229, 233, 233); </code></pre> <p>In my MySQL database, I have this code:</p> <pre><code>$data = mysql_query("SELECT amount FROM example") or die(mysql_error()); $arr = mysql_fetch_array($data); </code></pre> <p>The data is the same, the exception being that the MySQL data contains <code>.00</code> after each record. During the time spent trying to find out why it was giving the wrong results, I removed the <code>.00</code> from each record in the database. The data is still wrong though.</p> <p>The results, when using the PHP array gives me the following (correct) results:</p> <blockquote> <p>Average (mean) value of payments £146.4 <br /> Payments values that occur most often £233 <br /> Median value of payments £151</p> </blockquote> <p>The results, when using the MySQL array gives me the following (wrong) results:</p> <blockquote> <p>Average (mean) value of payments £144<br /> Payments values that occur most often £144<br /> Median value of payments £144</p> </blockquote> <p>I'm at a loss to explain why this is happening. Below is the PHP code I'm using to generate these results:</p> <pre><code> &lt;?php function arithmetic($array, $output = 'mean'){ switch($output){ // This case works out the mean case 'mean': $count = count($array); $sum = array_sum($array); $total = $sum / $count; break; // This case works out the median case 'median': rsort($array); $middle = round(count($array) / 2); $total = $array[$middle-1]; break; // This case works out the mode case 'mode': $v = array_count_values($array); arsort($v); foreach($v as $k =&gt; $v){$total = $k; break;} break; } return $total; } // PHP Array with data //$arr = array(60, 70, 71, 76, 144, 151, 197, 229, 233, 233); // MySQL Connection &amp; Retrieval of data $data = mysql_query("SELECT amount FROM example") or die(mysql_error()); $arr = mysql_fetch_array($data); ?&gt; </code></pre> <p>Anyone have any ideas?</p> <p><strong>UPDATE:</strong><br /> I've been playing the MySQL query, and using the following code:</p> <pre><code>$data = mysql_query("SELECT * FROM example") or die(mysql_error()); $arr = mysql_fetch_assoc($data); print_r ($arr); </code></pre> <p>It displays the following, only result:</p> <blockquote> <p>Array ( [id] => 1 [amount] => 144 )</p> </blockquote> <p>So really, the query is bringing all of the records and not just the one.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload