• Did you order new AirTags? We've opened a dedicated AirTags forum.

IDMah

macrumors 6502
Original poster
May 13, 2011
300
9
Hi all..

Trying to quash a bug in my High Score code and can't seem to figure it out.
I seem to be missing the first element in the mySQL database.

Here's the code that gets it. and Yes it's executed after the upload is complete.

Code:
- (void)getsScore
{
    // dispatched a AFNetworking in background and gets the highscores // 
    NSLog(@"Conneting to server");
    
    dispatch_async(kBgQueue, ^{
        NSData* data = [NSData dataWithContentsOfURL: 
                        theLatestHighScoreURL];
        [self performSelectorOnMainThread:@selector(fetchedData:) 
                               withObject:data waitUntilDone:YES];
        
    });
}

- (void)fetchedData:(NSData *)responseData {
    
    NSError *error = nil;
    NSMutableArray *jsonArray = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];
   
    NSMutableArray *hsEnteryArray = [[NSMutableArray alloc]init]; // arrayWithCapacity:[jsonArray count]];
  
    NSLog(@"JSON count: %i",[jsonArray count]);
    // so this loop is the problem //
   
    for (NSDictionary *hsDictionary in jsonArray) {
  
       HighScoreKeeper *tempHighScore = [[HighScoreKeeper alloc]retain];
       NSMutableString *gotName = [hsDictionary objectForKey:@"userName"];
       [tempHighScore setName:gotName]; 
       NSMutableString *gotTimestring = [hsDictionary objectForKey:@"userID"];
       [tempHighScore setGameTime:gotTimestring];
       NSMutableString *gotCountry = [hsDictionary objectForKey:@"country"];
       [tempHighScore setCountry:gotCountry];
       NSInteger gotTime = [[hsDictionary objectForKey:@"score"]integerValue];
       [tempHighScore setGameTimeSeconds:gotTime];
       
      [hsEnteryArray addObject:tempHighScore]; 
      NSLog(@"DB:%@ %i GsT %@ %@",gotName,gotTime,gotTimestring,gotCountry);
    }
    // save the data to NSDefault // 
    NSUserDefaults *defaults= [NSUserDefaults standardUserDefaults];
    NSData *worldEncodedObject = [NSKeyedArchiver archivedDataWithRootObject:hsEnteryArray];
    [defaults setObject:worldEncodedObject forKey:@"worldHighScoreTable"];
    
    SingletonCentre *myShareSingHS = [SingletonCentre sharedSingleton];
    
    // if I get the update then blank ASS-ume all the queued Items have been sent //
    if(myShareSingHS.queuedItems == NO)
    {
        
        // Reset the Not Highscore Flag to avoid double entries //
         // Upddate the view tells the tableview to update and reload data // 
        [[NSNotificationCenter defaultCenter] postNotificationName:@"PLEASEVIEWTOUPDATE" object:nil];
    }
    
}

output reads:
Code:
2013-11-17 15:20:32.686 UIScrollView-Paging-networked[2076:207] JSON count: 4
2013-11-17 15:20:32.686 UIScrollView-Paging-networked[2076:207] DB:mayaName 66 GsT 00:01:06 
2013-11-17 15:20:32.686 UIScrollView-Paging-networked[2076:207] DB:DawnBooby 66 GsT 00:01:06 
2013-11-17 15:20:32.687 UIScrollView-Paging-networked[2076:207] DB:Michel 66 GsT 00:01:06 
2013-11-17 15:20:32.687 UIScrollView-Paging-networked[2076:207] DB:AxelJoh 126 GsT 00:02:16 Auckland,New Zealnad
2013-11-17 15:20:32.687 UIScrollView-Paging-networked[2076:207] Saving:mayaName- 00:01:06 66 at (null)
2013-11-17 15:20:32.687 UIScrollView-Paging-networked[2076:207] Saving:DawnBooby- 00:01:06 66 at (null)
2013-11-17 15:20:32.688 UIScrollView-Paging-networked[2076:207] Saving:Michel- 00:01:06 66 at (null)
2013-11-17 15:20:32.689 UIScrollView-Paging-networked[2076:207] Saving:AxelJoh-Auckland,New Zealnad 00:02:16 126 at (null)
2013-11-17 15:20:32.700 UIScrollView-Paging-networked[2076:207] pinged by 1
2013-11-17 15:20:32.700 UIScrollView-Paging-networked[2076:207] reloading data
2013-11-17 15:20:32.700 UIScrollView-Paging-networked[2076:207] loading: mayaName -  time:00:01:06 - 66 at:(null)
2013-11-17 15:20:32.701 UIScrollView-Paging-networked[2076:207] loading: DawnBooby -  time:00:01:06 - 66 at:(null)
2013-11-17 15:20:32.701 UIScrollView-Paging-networked[2076:207] loading: Michel -  time:00:01:06 - 66 at:(null)
2013-11-17 15:20:32.701 UIScrollView-Paging-networked[2076:207] loading: AxelJoh - Auckland,New Zealnad time:00:02:16 - 126 at:(null)

and the Database reads:
Code:
44		00:01:06	Michelle	66	0 <-------- always missing first element ------
45		00:01:06	mayaName	66	0
43		00:01:06	DawnBooby	66	0
42		00:01:06	Michel	66	0
41		00:02:16	AxelJoh	126	Auckland,New Zealnad			0

*** Sorry forgot to include the php ***
Oops probably need the php too.
Code:
<?php
require("config.php");
// to use sqli_query 
$link = mysqli_connect("localhost","HighAdmin","password","HighScorerTable") or die("Error " . mysqli_error($link));

// now get the data from the database
//$result = mysqli_query("SELECT * from HighScore_elements_global") or die('query failed');

//  creates a query string  
$query = "SELECT * FROM HighScore_elements_global" or die("Error in the consult.." . mysqli_error($link));
$result = mysqli_query($link,$query,MYSQLI_STORE_RESULT);
//if(mysqli_num_rows($result)){
if($result->num_rows){
    echo '[';
    $first = true;
    $row=mysqli_fetch_assoc($result);
    while($row=mysqli_fetch_array($result,MYSQL_ASSOC)){
    	// mysqli_fetch_row($result)
        //  cast results to specific data types
	    //  needs to output something like: "status": "fundraising","funded_amount": 25,"basket_amount": 50,
	    //  This is so Xcode can ingest the data to a NSDictonary  
        if($first) {
            $first = false;
        } else {
           echo ',';
        }
        echo json_encode($row);
    }
    echo ']';
} else {
    echo '[]';
}

mysqli_close($link);


thanks
Ian
 
Last edited:

Sonnestah

macrumors regular
Mar 2, 2013
152
0
Why do you think the problem is your loop? Couldn't the problem be your php code?
 
Comment

IDMah

macrumors 6502
Original poster
May 13, 2011
300
9
Sorry forgot to include the php.. which I'm just learning for this.

also this site keeps going up and down if I use Safari don't know why.
Have notified the authorities.. haha!!

thanks for any help!!
Ian

ps. more than likely you are right it's the php but have no idea where..
 
Comment

chown33

Moderator
Staff member
Aug 9, 2009
9,188
5,776
Hellanback
I don't know PHP, but just using basic logic I think you have a few problems.

First:
Code:
// now get the data from the database
//$result = mysqli_query("SELECT * from HighScore_elements_global") or die('query failed');

//  creates a query string  
$query = "SELECT * FROM HighScore_elements_global" or die("Error in the consult.." . mysqli_error($link));
$result = mysqli_query($link,$query,MYSQLI_STORE_RESULT);
If the uncommented code is supposed to be logically equivalent to the commented code, it almost certainly isn't.

The 'or die...' clause should be appended to the $result = mysqli_query... expression. It's illogical when it's appended to the assignment of a constant string.

By illogical, I mean that it doesn't make any sense, and that it doesn't logically correspond. In order for the following to be correct:
Code:
$query = "SELECT * FROM HighScore_elements_global" or die("Error in the consult.." . mysqli_error($link));
it would need something like this in the commented code:
Code:
//$result = mysqli_query( "SELECT * from HighScore_elements_global" or die('query failed') );
Note carefully what's inside the parens, and compare with the original commented-out code.


Second, look carefully at this code:
Code:
    $row=mysqli_fetch_assoc($result);
    while($row=mysqli_fetch_array($result,MYSQL_ASSOC)){
It makes two assignments to the $row variable, with no intervening action performed on the result of mysqli_fetch_assoc(). Even knowing nothing about PHP, this strikes me as unlikely to be correct.

I have no idea what the correct code would be here, since I have no idea what either of the mysqli_XXX funcs returns. If mysqli_fetch_assoc() returns the first row, or advances the cursor in the result-set, it would be completely consistent with your observed misbehavior.


You probably need to learn at least some PHP, or find a better example to work from. I also suggest setting up some test cases, or diagnostics like printing the number of rows in the result-set (probably the value $result->num_rows).
 
Comment

IDMah

macrumors 6502
Original poster
May 13, 2011
300
9
Thanks for the suggestion.

Changed it. Didn't help anything, but what ever.

Ended up rewriting all the php database listing code based on: http://us1.php.net/mysqli_fetch_assoc

The key was this. php code.

Code:
$result = mysqli_query($link,$query,MYSQLI_STORE_RESULT) or die("Error in the consult.." . mysqli_error($link));

while ($row = mysqli_fetch_assoc($result)) {
      	 if($first) {
            $first = false;
        } else {
           echo ',';
        }
      	 echo json_encode($row);
}

Hope this helps someone else.
thanks
Ian
 
Comment
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.