Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    Regular Coder
    Join Date
    Oct 2009
    Posts
    410
    Thanks
    6
    Thanked 3 Times in 3 Posts

    mysqli - unable to use all results, even though the count shows 48 of them

    For some reason I am unable to get all of the results in $getResults

    In the code below I have commented what the output is for the lines that do output.

    The
    foreach($results as $result) {
    should be going through each of the 48 results, but only does one of them and stops.

    I can not figure out why it is missing the rest of them, even though the first output shows that there are 48 to do.

    Can anyone spot what I maybe doing wrong ?

    Code:
    $getResults = db_query($mysqli, "SELECT `photos` FROM `items` WHERE `photos` != ''");
    // 48 results are returned if i enter the query directly in to phpmyadmin.
    
    
    print_r($getResults);
    //		mysqli_result Object ( [current_field] => 0 [field_count] => 1 [lengths] => [num_rows] => 48 [type] => 0 ) Array ( [photos] => 123456.jpg|| )
    // correct
    
    
    $results = $getResults->fetch_assoc();
    
    echo("<br><br><br>:");
    print_r($results);	//		Array ( [photos] => 123456.jpg|| )
    echo(":<br>");
    // SHOULD BE SHOWING ALL RESULTS IN THE RETURNED RESULTS ARRAY.  BUT ONLY SHOWS ONE.
    
    $exist_imgs = glob('/home/****/public_html/images/L*.jpg');
    $exist_tn = glob('/home/****/public_html/images/S*.jpg');
    $missing_imgs = "";
    ?><br><br>
    total<br>
    db=<? echo($getResults->num_rows); ?><br>
    img=<? echo(count($exist_imgs)); ?><br>
    tn=<? echo(count($exist_tn)); ?><br><br>
    
    	// check each image in DB and remove from the arrays if imgs and tn exist.
    	foreach($results as $result) {
    	$photos = explode("||", $result);
    			foreach($photos as $key) {
    			if ($key) {
    			echo("testing: ". $key . "<br><br>");
    			// Check if TN or Pid exists.
    			$tn_id_Exists = file_exists("/home/****/public_html/images/S" . $key . "");
    				if ($tn_id_Exists) {
    				//	remove entry for this image as it exists.
    				
    				} else {
    				$missing_imgs .= "S" . $key . "<br>";
    				}
    			$img_id_Exists = file_exists("/home/****/public_html/images/L" . $key . "");
    				if ($img_id_Exists) {
    				//	remove entry for this image as it exists.
    				
    				} else {
    				$missing_imgs .= "L" . $key . "<br>";
    				}
    			}
    			}
    	?><br>----------------------------------<br><?
    	}
    Last edited by needsomehelp; 02-12-2013 at 05:31 PM.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,980
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    What version of PHP are you using? 5.4 is required if you intend to use Traversable on the mysqli_result (ie: the for each call).
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #3
    Regular Coder
    Join Date
    Oct 2009
    Posts
    410
    Thanks
    6
    Thanked 3 Times in 3 Posts
    I have PHP Version 5.3.20.

    I have just looked at the original code I use to use for this when I used mysql and see that I should be using the assoc for each row.

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,980
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Yes, you can use a while loop, but not a foreach unless you upgrade to 5.4.
    I wish they wouldn't change something like Traversable unless its in a major version; minor versions simply create confusion.

    Edit:
    Wait I'm wrong here. The foreach is already the results of a fetch_assoc call. I thought that was the result of the mysqli_result.
    In that case, you're simply missing your while loop. Looks to me that you'll likely have a normalization issue as well since I see you exploding on ||.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #5
    Regular Coder
    Join Date
    Oct 2009
    Posts
    410
    Thanks
    6
    Thanked 3 Times in 3 Posts
    Still the same, it only uses the first returned results.
    The foreach does not advance to the next row.

    Code:
    $getResults = db_query($mysqli, "SELECT `photos` FROM `items` WHERE `photos` != ''");
    // 48 results are returned if i enter the query directly in to phpmyadmin.
    
    
    print_r($getResults);
    //		mysqli_result Object ( [current_field] => 0 [field_count] => 1 [lengths] => [num_rows] => 48 [type] => 0 ) Array ( [photos] => 123456.jpg|| )
    
    
    
    
    
    $exist_imgs = glob('/home/****/public_html/images/L*.jpg');
    $exist_tn = glob('/home/****/public_html/images/S*.jpg');
    $missing_imgs = "";
    ?><br><br>
    total<br>
    db=<? echo($getResults->num_rows); ?><br>
    img=<? echo(count($exist_imgs)); ?><br>
    tn=<? echo(count($exist_tn)); ?><br><br>
    <?
    
    
    // check each image in DB and remove from the arrays if imgs and tn exist.
    foreach($getResults->fetch_assoc() as $results) {
    	//$result = $results;
    	echo("<br><br><br>:");
    	print_r($results);	//		Array ( [photos] => 123456.jpg|| )
    	echo(":<br>");
    	//		
    $photos = explode("||", $results);
    		foreach($photos as $key) {
    		if ($key) {
    		echo("testing: ". $key . "<br><br>");
    			// Check if TN or Pid exists.
    			$tn_id_Exists = file_exists("/home/****/public_html/images/S" . $key . "");
    					if ($tn_id_Exists) {
    					//	remove entry for this image as it exists.
    					
    					} else {
    					$missing_imgs .= "S" . $key . "<br>";
    					}
    			$img_id_Exists = file_exists("/home/****/public_html/images/L" . $key . "");
    					if ($img_id_Exists) {
    					//	remove entry for this image as it exists.
    					
    					} else {
    					$missing_imgs .= "L" . $key . "<br>";
    					}
    		}
    		}
    		?><br>----------------------------------<br><?
    }
    Last edited by needsomehelp; 02-12-2013 at 06:29 PM.

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,980
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Read the edit I put up there, you're missing your while loop. You cannot loop a resultset using a foreach in 5.3 and mysqli_result.
    I misunderstood the purpose of the foreach you have here.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #7
    Regular Coder
    Join Date
    Oct 2009
    Posts
    410
    Thanks
    6
    Thanked 3 Times in 3 Posts
    that did it, a few tweaks to the code later and it is not showing the other results.

    I'll have to keep that in mind for now that I need to use WHILE for getting the next row of results.

    Thank you for your help.

  • #8
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,980
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Mkay, so you got this working then or no?
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #9
    Regular Coder
    Join Date
    Oct 2009
    Posts
    410
    Thanks
    6
    Thanked 3 Times in 3 Posts
    yes, all working now. thanks again, and best of all it is less than half the code i started with after i had a rethink on the method i was using to locate missing images.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •