1

The following code gives me the following:

$getexpenses = "SELECT sum(expense_amount) FROM projects_expense WHERE project_id=$project_id";     
    $showexpenses = @mysqli_query ($dbc, $getexpenses); // Run the query.

    echo ' . $showexpenses . ';

Gives me a result of

' . $showexpenses . '

instead of the actual sum...

I'm sure it's something simple I'm missing... thanks!

6
  • btw echoing the result of mysqli_query()won't be very useful. You need to treat this with something like mysqli_fetch_assoc(); Commented Apr 27, 2011 at 19:23
  • Okay - I get the single/double quotes thing... but neither is working? Single quotes prints nothing and double quotes prints . . Commented Apr 27, 2011 at 19:24
  • You need to read my comment... Commented Apr 27, 2011 at 19:28
  • Sorry , I didn't see you had left a comment. I tried simply replacing the _query with _fetch_assoc but that didn't work Commented Apr 27, 2011 at 19:30
  • @user517593 Is your problem now fixed? Commented Apr 27, 2011 at 19:34

4 Answers 4

2

echo " . $showexpenses . " will work for you. You need double quotes, not single.

Sign up to request clarification or add additional context in comments.

Comments

1

I added a call to mysqli_fetch_assoc($showexpenses)to make it fully functional. I also sanitized $project_id to avoid injections and used an alias to make the sum accessible through an associative array.

$getexpenses = "SELECT SUM(`expense_amount`) as `sum_amount` FROM `projects_expense` WHERE `project_id`= ".intval($project_id);     
$showexpenses = mysqli_query ($dbc, $getexpenses);
$sums = mysqli_fetch_assoc($showexpenses);
$sum = $sums['sum_amount'];

echo $sum;

We could also have used mysqli_fetch_row like this:

$getexpenses = "SELECT SUM(`expense_amount`) FROM `projects_expense` WHERE `project_id`= ".intval($project_id);     
$showexpenses = mysqli_query ($dbc, $getexpenses);
$sums = mysqli_fetch_row($showexpenses);
$sum = $sums[0];

echo $sum;

The alias is not needed anymore since we know the first column (0) is a match.

NB: you probably also need to GROUP BY project_id if you want to use SUM()

Comments

1

use

   echo $showexpenses;

or

echo "My query is: {$showexpenses}";

Comments

0

Further to Nik's response, PHP treats strings differently based on whether you use Single Quotes or Double Quotes around them.

Single-Quotes interpret text as literal text except for '\'' which would output a single quote mark and '\' which would output a slash mark. For this reason, using single quotes would output your variable name instead of the value.

Examples:

CODE             | OUTPUT
-----------------+------------------
echo '\t';       | \t
echo '\r';       | \r
echo '$foo';     | $foo

Double-Quotes interpret certain escape sequences for special characters and also interpret variables. For instance, "\n" ouputs a linefeed and "\r" outputs a carriage return. For this reason, echoing "$myvariable" outputs the value instead of the variable name.

Examples:

CODE             | OUTPUT
-----------------+------------------
echo "\t";       | {tab space}
echo "\r";       | {carriage return}
echo "$foo";     | bar

More reading: http://www.php.net/manual/en/language.types.string.php

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.