Problem with scope

Hey

I’m creating a variable inside a for loop.
Well, I’m assigning a value to

$_SESSION['totalsum'] = $_SESSION['totalsum'] + $sum;

After this I call a function which needs this info, but when I echo this, it seems to have lost it’s value. I know SESSION is a superglobal so I don’t need to use global inside function on it. What could be the problem? The for loop is inside an if statement, and it seems to exist in there but inside the function it doesn’t.
I tried changing the order in which the function is declared to before and after, but nothing is happening.

I actually want the client_id, to insert it into an other table as foreign key.
I know I may get same names, I may add a email column to the table later.

What I mean is that I want:

$client_id = mysqli_query($dbc, $query2) or die('No data');

$client_id to equal a number.

I still think you are going about it the wrong way.

But if you insist, you must use mysqli fetch row, no need for a loop because you are only fetching the first row:

http://php.net/manual/en/mysqli-result.fetch-row.php

And like I said, you LIMIT clause to limit the results to one row, to help speed up the query.

You are retreiving one column.

Do you mean one row? You can use the LIMIT clause.

May I suggest you retrieve the data with a primary key. What happens if you have multiple clients with the same name?

Ah, I got it working.
One offtopic question:

How do I get only one column from MySQL,


$query2 = "SELECT client_id FROM clients WHERE firstname = '$firstname' AND lastname = '$lastname'";
$client_id = mysqli_query($dbc, $query2) or die('No data');

This doesn’t work, what should I do? While loop with fetch array?

Show us the function you are using to help you.

Initialize it outside the loop. $_SESSION[‘totalsum’] = 0;