Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9030397
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T07:28:52+00:00 2026-06-16T07:28:52+00:00

Good evening all. I’m currently working on a small personal project. It’s purpose is

  • 0

Good evening all.

I’m currently working on a small personal project. It’s purpose is to retrieve numerous values from a database on my backend and store them as variables. These variables are then used to modify the appearance of some HTML5 Canvas objects (in this case, i’m using arcs).

Please note that the values in the database are Text and thus my bind statements refer to that. The queries i’m calling (AVG, MIN, MAX) work fine with the values i’ve got as the fields store numerical data (this is merely due to another script that deals with adding or updating the data — that’s already running MySQLi, and using Text was the best solution for my situation).

Now, i achieved what i wanted with standard MySQL queries, but it’s messy code and the performance of it could prove to be terrible as the database grows. For that reason, i want to use loops. I also feel that bind_param of MySQLi would be much better for security. The page doesn’t accept ANY user input, it’s merely for display and so injection is less of a concern, but at some point in the future, i’ll be looking to expand it to allow users to control what is displayed.

Here’s a sample of my original MySQL PHP code sample;

$T0A = mysql_query('SELECT AVG(Temp0) FROM VTempStats'); // Average
$T0B = mysql_query('SELECT MIN(Temp0) FROM VTempStats'); // Bottom/MIN
$T0T = mysql_query('SELECT MAX(Temp0) FROM VTempStats'); // Top/MAX
$T1A = mysql_query('SELECT AVG(Temp1) FROM VTempStats'); // Average
$T1B = mysql_query('SELECT MIN(Temp1) FROM VTempStats'); // Bottom/MIN
$T1T = mysql_query('SELECT MAX(Temp1) FROM VTempStats'); // Top/MAX

$r_T0A = mysql_result($T0A, 0);
$r_T0T = mysql_result($T0T, 0);
$r_T0B = mysql_result($T0B, 0);
$r_T1A = mysql_result($T1A, 0);
$r_T1T = mysql_result($T1T, 0);
$r_T1B = mysql_result($T1B, 0);

if ($r_T0A == "" ) {$r_T0A = 0;}
if ($r_T1A == "" ) {$r_T1A = 0;}

if ($r_T0B == "" ) {$r_T0B = 0;}
if ($r_T1B == "" ) {$r_T1B = 0;}

if ($r_T0T == "" ) {$r_T0T = 0;}
if ($r_T1T == "" ) {$r_T1T = 0;}

That’s shorter than the original, as there’s 4×3 sets of queries (Temp0,Temp1,Temp2,Temp3, and min,max,avg for each). Note that the last 6 if statements are merely there to ensure that fields that are null are automatically set to 0 before my canvas script attempts to work with them (see below).

To show that value on the arc, i’d use this in my canvas script (for example);

var endAngle = startAngle + (<?= $r_T0A ?> / 36+0.02);

It worked for me, and what was displayed was exactly what i expected.

Now, in trying to clean up my code and move to a loop and MySQLi, i’m running into problems. Being very new to both SQL and PHP, i could use some assistance.

This is what i tried;

$q_avg = "SELECT AVG(Temp?) FROM VTempStats";
    for ($i_avg = 0; $i_avg <= 3; ++$i_avg)
    {
        if ($s_avg = $mysqli->prepare($q_avg))
        {
            $s_avg->bind_param('s',$i_avg);
            $s_avg->execute();
            $s_avg->bind_result($avg);
            $s_avg->fetch();
            echo $avg;
        }
    }

Note: mysqli is the MySQLi connection. I’ve cut the code down to only show the AVG query loop, but the MIN and MAX loops are nearly identical.

Obviously, that won’t work as it’s only assigning one variable for each set of queries, instead of 4 variables for each loop.

As you can imagine, what i want to do is assign all 12 values to individual variables so that i can work with them in my canvas script. I’m not entirely sure how i go about this though.

I can echo individual values out through MySQLi, or i can query the database to change or add data through MySQLi, but trying to make a loop that does what i intend with MySQLi (or even MySQL), that’s something i need help with.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T07:28:54+00:00Added an answer on June 16, 2026 at 7:28 am

    From my reading of your code, you have a fixed number of columns and know their names, and you are applying the AVG(), MIN(), MAX() aggregates to the same table over the same aggregate group, with no WHERE clause applied. Therefore, they can all be done in one query from which you just need to fetch one single row.

    SELECT
      AVG(Temp0) AS a0,
      MIN(Temp0) AS min0,
      MAX(Temp0) AS max0,
      AVG(Temp1) AS a1,
      MIN(Temp1) AS min1,
      MAX(Temp1) AS max1,
      AVG(Temp2) AS a2,
      MIN(Temp2) AS min2,
      MAX(Temp2) AS max2,
      AVG(Temp3) AS a3,
      MIN(Temp3) AS min3,
      MAX(Temp3) AS max3
    FROM VTempStats
    

    This can be done in a single call to $mysqli->query(), and no parameter binding is necessary so you don’t need the overhead of prepare(). One call to fetch_assoc() is needed to retrieve a single row, with columns aliased like a0, min0, max0, etc... as I have done above.

    // Fetch one row
    $values = $result_resource->fetch_assoc();
    print_r($values);
    printf("Avg 0: %s, Min 0: %s, Max 0: %s... etc....", $values['a0'], $values['min0'], $values['max0']);
    

    These can be pulled into the global scope with extract(), but I recommend against that. Keeping them in their $values array makes their source more explicit.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Good evening all, I've been working on an MD5 tool in C# that takes
Good evening guys. I'm currently trying to get started on development of a project
Good evening all! i have an Ajax call that loads 5 'echo's' from a
Good evening all! In a current project, i'm experiencing a rather worrying memory leak
Good evening, I'm working on a project that has a list of seven options
Good evening, I'm having an issue with Masonry. This is all my relevant code:
Good evening, In my app that I'm currently developing, I have a class that
Good evening, I am working on a program where some application config info is
Good Evening All, I've created the following stored procedure: CREATE PROCEDURE AddQuote -- Add
Good evening! Been searching all over, and I'm afraid I've just got something completely

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.