Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // Connect to MySQL
- $conn = mysqli_connect('host', 'username', 'password', 'database');
- // Define the number of bins
- $bins = 10;
- // Select the minimum and maximum values from the column
- $result = mysqli_query($conn, "SELECT MIN(col) AS min, MAX(col) AS max FROM table");
- $row = mysqli_fetch_assoc($result);
- $min = $row['min'];
- $max = $row['max'];
- // Calculate the bin size
- $bin_size = ($max - $min) / $bins;
- // Initialize an array to hold the bin frequencies
- $bin_freqs = array_fill(0, $bins, 0);
- // Select the values from the column and assign them to bins
- $result = mysqli_query($conn, "SELECT col FROM table");
- while ($row = mysqli_fetch_assoc($result)) {
- $col = $row['col'];
- $bin = floor(($col - $min) / $bin_size);
- $bin_freqs[$bin]++;
- }
- // Print the histogram
- for ($i = 0; $i < $bins; $i++) {
- $bin_left = $min + $i * $bin_size;
- $bin_right = $bin_left + $bin_size;
- echo "$bin_left - $bin_right: $bin_freqs[$i]\n";
- }
- // Close the connection
- mysqli_close($conn);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement