CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   MySQL (http://www.codingforums.com/forumdisplay.php?f=7)
-   -   mysqli - unable to use all results, even though the count shows 48 of them (http://www.codingforums.com/showthread.php?t=287522)

needsomehelp 02-12-2013 04:52 PM

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><?
        }


Fou-Lu 02-12-2013 06:02 PM

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).

needsomehelp 02-12-2013 06:20 PM

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.

Fou-Lu 02-12-2013 06:22 PM

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 ||.

needsomehelp 02-12-2013 06:26 PM

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><?
}


Fou-Lu 02-12-2013 06:31 PM

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.

needsomehelp 02-12-2013 06:46 PM

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.

Fou-Lu 02-12-2013 06:52 PM

Mkay, so you got this working then or no?

needsomehelp 02-12-2013 07:14 PM

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.


All times are GMT +1. The time now is 11:27 PM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.