Question: I own an rpg and want to add up all the levels of the users monster and get an end number.

So I narrowed it down for one user and works great.

```
$_SESSION['username'] = "smbisme";

$query = "SELECT belongsto, SUM(level) FROM user_pokemon WHERE `belongsto` = '".$_SESSION['username']."' "; 

$result = mysql_query($query) or die(mysql_error());

// Print out result
while($row = mysql_fetch_array($result)){
    echo $row['belongsto'];
    echo "". $row['SUM(level)'];
    echo "<br />";
}

```

Now, that shows and works for the user 'smbisme' but now I want to do it for all users.

So I try and take the where out like so.

```
$query = "SELECT belongsto, SUM(level) FROM user_pokemon  "; 

$result = mysql_query($query) or die(mysql_error());

// Print out result
while($row = mysql_fetch_array($result)){
    echo $row['belongsto'];
    echo "". $row['SUM(level)'];
    echo "<br />";
}

```

But then for some reason i am only getting the highest result ( 1 result ) i would like to do what i did for the 1 user but grab all of the results instead of just the one user.

Answer: 
Make a copy of a suitable number of characters:

```
char * substr = malloc(index + 2);
strncpy(substr, ch, index + 1);
substr[index + 1] = 0;

// ...

free(substr);

```

If you're happy to mutilate the original string, just insert a null byte:

```
ch[index + 1] = 0;

```

The odd `+ 1` comes from the fact that your index seems to be *inclusive*, which is generally a [bad idea](https://stackoverflow.com/a/9963438/596781).
the same behavior of java substring (allocates new string)

```
 char* res = (char*)malloc(index+2);
 strncpy(res,ch,index+1);
 res[index+1]='\0';

```

I see that you try to delimit by +, so easier is to use strtok :

```
 char ch[] ="323+465";
 char * res;
 res = strtok (ch,"+");
 // res= 323

```