Go Back   CodingForums.com > :: Server side development > MySQL

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-12-2013, 04:52 PM   PM User | #1
needsomehelp
Regular Coder

 
Join Date: Oct 2009
Posts: 302
Thanks: 4
Thanked 3 Times in 3 Posts
needsomehelp can only hope to improve
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..
needsomehelp is offline   Reply With Quote
Old 02-12-2013, 06:02 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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).
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Old 02-12-2013, 06:20 PM   PM User | #3
needsomehelp
Regular Coder

 
Join Date: Oct 2009
Posts: 302
Thanks: 4
Thanked 3 Times in 3 Posts
needsomehelp can only hope to improve
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.
needsomehelp is offline   Reply With Quote
Old 02-12-2013, 06:22 PM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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 ||.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Old 02-12-2013, 06:26 PM   PM User | #5
needsomehelp
Regular Coder

 
Join Date: Oct 2009
Posts: 302
Thanks: 4
Thanked 3 Times in 3 Posts
needsomehelp can only hope to improve
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..
needsomehelp is offline   Reply With Quote
Old 02-12-2013, 06:31 PM   PM User | #6
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Old 02-12-2013, 06:46 PM   PM User | #7
needsomehelp
Regular Coder

 
Join Date: Oct 2009
Posts: 302
Thanks: 4
Thanked 3 Times in 3 Posts
needsomehelp can only hope to improve
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.
needsomehelp is offline   Reply With Quote
Old 02-12-2013, 06:52 PM   PM User | #8
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Mkay, so you got this working then or no?
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Old 02-12-2013, 07:14 PM   PM User | #9
needsomehelp
Regular Coder

 
Join Date: Oct 2009
Posts: 302
Thanks: 4
Thanked 3 Times in 3 Posts
needsomehelp can only hope to improve
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.
needsomehelp is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:20 AM.


Advertisement
Log in to turn off these ads.