HiveBrain v1.2.0
Get Started
← Back to all entries
patternphpMinor

Need help optimizing PHP/MySQL code snippet

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
snippetneedphphelpmysqloptimizingcode

Problem

I am sorry for the simple request, but I'm trying to figure out the best way to optimizing the following bit of code. I hate using 20 lines of code when 12-15 will suffice. I also don't like having to use intermediate variables if I don't need to.

$total_query_raw = "SELECT SUM( total ) AS total
                        FROM  `tblinvoices` 
                        WHERE  `datepaid` 
                            BETWEEN  '$start_date 00:00:00' AND  '$end_date 23:59:59'";

$total_query = mysqli_query($link, $total_query_raw);
    if (!$total_query) {
        echo "DB Error, could not query the database\n";
        echo 'MySQL Error: ' . mysqli_error();
        exit;
    }
$total_result = mysqli_fetch_array($total_query);
$grand_total = $total_result['total'];
echo "Grand Total: $" . number_format($grand_total,2) . "";
mysqli_free_result($total_query);

Solution

you might want to write it like this

$total_query_raw = "SELECT SUM( total ) AS total
                        FROM  `tblinvoices` 
                        WHERE  `datepaid` 
                            BETWEEN  '$start_date 00:00:00' AND  '$end_date 23:59:59'";

$total_query = mysqli_query($link, $total_query_raw);

if (!$total_query) {
    echo "DB Error, could not query the database\n";
    echo 'MySQL Error: ' . mysqli_error();
    exit;
} else {
    $total_result = mysqli_fetch_array($total_query);
    $grand_total = $total_result['total'];
    echo "Grand Total: $" . number_format($grand_total,2) . "";
    mysqli_free_result($total_query);
}


otherwise you are going to try and perform action on something that you know will give you an error, that is the whole reason for the if statement, so put the rest of that stuff in the else statement.

and I am not sure but I think you can write the last part of it like this

I think that it will do the assignment inside the if statement and return a 1 or 0 (which relates to true or false)

but I may be thinking of another language that allows you to do this

if ($total_query = mysqli_query($link, $total_query_raw))
{
    $total_result = mysqli_fetch_array($total_query);
    $grand_total = $total_result['total'];
    echo "Grand Total: $" . number_format($grand_total,2) . "";
    mysqli_free_result($total_query);
} 
else 
{
    echo "DB Error, could not query the database\n";
    echo 'MySQL Error: ' . mysqli_error();
    exit;
}


UPDATE

ok so I did a little research on the Declaration & Assignment inside the condition, and based of THIS ANSWER you can perform the assignment inside the If Condition.

it does sound like you have to assign a "truthy" value? not sure exactly what that means. I assume that if the assignment is good, it will return True but if the assignment is bad it will return False.

Code Snippets

$total_query_raw = "SELECT SUM( total ) AS total
                        FROM  `tblinvoices` 
                        WHERE  `datepaid` 
                            BETWEEN  '$start_date 00:00:00' AND  '$end_date 23:59:59'";

$total_query = mysqli_query($link, $total_query_raw);

if (!$total_query) {
    echo "DB Error, could not query the database\n";
    echo 'MySQL Error: ' . mysqli_error();
    exit;
} else {
    $total_result = mysqli_fetch_array($total_query);
    $grand_total = $total_result['total'];
    echo "Grand Total: $" . number_format($grand_total,2) . "<br>";
    mysqli_free_result($total_query);
}
if ($total_query = mysqli_query($link, $total_query_raw))
{
    $total_result = mysqli_fetch_array($total_query);
    $grand_total = $total_result['total'];
    echo "Grand Total: $" . number_format($grand_total,2) . "<br>";
    mysqli_free_result($total_query);
} 
else 
{
    echo "DB Error, could not query the database\n";
    echo 'MySQL Error: ' . mysqli_error();
    exit;
}

Context

StackExchange Code Review Q#37703, answer score: 4

Revisions (0)

No revisions yet.