# Need help optimizing PHP/MySQL code snippet

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) . "<br>"; mysqli_free_result($total_query);


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) . "<br>"; 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) . "<br>"; 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.

• I am curious, did the Declaration in the If Condition work? – Malachi Dec 19 '13 at 14:18
• Technically, PHP variables aren't declared, they just magically appear out of nowhere. Your way of initializing the variable inside the if-statement should work fine and is very common in PHP. – Simon Forsberg Dec 19 '13 at 14:58
• @SimonAndréForsberg thank you for that little bit of info, I forgot about PHP being so nice (like Javascript) and giving you whatever you want on the fly like that. – Malachi Dec 19 '13 at 15:20
• I'm not so sure I would call it "nice" but that's an entirely different subject... – Simon Forsberg Dec 19 '13 at 15:31