Solved

Storing query results in array

Posted on 2011-02-22
7
221 Views
Last Modified: 2012-05-11
Hi;
THis is how I am trying to store my values in to an array but the array does not return results would you please check it out see where I am making a mistale please.

Thanks a lot for the help
function fetch_array($query) {
		$result = mysql_fetch_array($query);
		return $result;
	}

Function get_associations($cat_ids) {
 
$db = mysql_select_db($database_Remoteiran79, $Remoteiran79);
$query_associations = ("SELECT * FROM rss_associations WHERE cat_id IN ($cat_ids)");
$rsAssociations  = mysql_query($query_rsAssociations , $Remoteiran79) or die(mysql_error());
$row_rsAssociations  = mysql_fetch_array($rsAssociations);
$totalRows_rsAssociations  = mysql_num_rows($rsAssociations);
		while ($associations = $db->fetch_array($query_associations)) {
			// create an array of the sources needed
			$sources[] = $rsAssociations[source_id];	
		}
		if (empty($sources)) { 
			$sources = "0"; 	
		}
		else {			
			$sources = implode(", ", $sources);			
		}
		return $sources;	 }
echo ($sources);

Open in new window

0
Comment
Question by:babak62
  • 3
  • 3
7 Comments
 
LVL 8

Expert Comment

by:ropenner
ID: 34957457
error:
     while ($associations = $db->fetch_array($query_associations))

you need to send the results to mysql_fetch_array (via your fetch_array function)
since you already read a row in line 11 you would need to do this before your while loop:  Although I'm not sure what the purpose of that first $row_rsAssociations is ... perhaps comment it out ... then you don't need to 'data seek' as shown below.
       mysql_data_seek ($rsAssociations,0)

then the while would look like
       while ($associations = fetch_array($rsAssociations)) {
          var_dump($associations);
       }

It would be best to rename $query to $results_handle in the function fetch_array and then it would be more accurate

the var_dump will show you what is in the array $associations, which will be one row of data from your result set, for each iteration of the while loop.
0
 
LVL 17

Expert Comment

by:Dushan De Silva
ID: 34957475
Try
9. $query_associations = ("SELECT * FROM rss_associations WHERE cat_id IN" . $cat_ids);

http://www.w3schools.com/PHP/php_mysql_select.asp
0
 

Author Comment

by:babak62
ID: 34957749
ropnner;
Thanks for the answer this is how I changed it based on what you wrote and I hope that I understood you right but after upload my page went blank also nothing there.  Would you please check to see if I did not miss something.

Thanks a lot
function fetch_array($results_handle) {
		$result = mysql_fetch_array($results_handle);
		return $result;
	}

Function get_associations($cat_ids) {
 
$db = mysql_select_db($database_Remoteiran79, $Remoteiran79);
$query_associations = ("SELECT * FROM rss_associations WHERE cat_id IN ($cat_ids)");
$rsAssociations  = mysql_query($query_rsAssociations , $Remoteiran79) or die(mysql_error());
//$row_rsAssociations  = mysql_fetch_array($rsAssociations);
$totalRows_rsAssociations  = mysql_num_rows($rsAssociations);
		
		while ($associations = fetch_array($rsAssociations)) {
          var_dump($associations);
       }
			// create an array of the sources needed
			$sources[] = $rsAssociations[source_id];	
		}
		if (empty($sources)) { 
			$sources = "0"; 	
		}
		else {			
			$sources = implode(", ", $sources);			
		}
		return $sources;	 
echo ($sources);

Open in new window

0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 8

Expert Comment

by:ropenner
ID: 34957846
the var_dump was meant just for testing purposes and the extra  bracket you left in after it would cause problems. I'd comment it out after testing with it.

the while loop would actually be:

while ($associations = mysql_fetch_assoc($rsAssociations)) {
     $sources[] = $associations{"source_id"};
}

it looks like you are trying to put the FIELD == "source_id"  into the $sources[] array.  If so then the mysql_fetch_assoc would be best I think and then you don't need the function "fetch_array".  You can use either {} brackets or [] brackets to reference an item in an associative array. so

     $sources[] = $associations["source_id"];

would also be valid.

If you want to see why the screen was blank then you can go to your log files of your web server and look there for the PHP error that will tell you that the extra bracket is there.
0
 

Author Comment

by:babak62
ID: 34958003
OK I put this in my page but the page came out empty


Function get_associations($cat_ids) {
 
$db = mysql_select_db($database_Remoteiran79, $Remoteiran79);
$query_associations = ("SELECT * FROM rss_associations WHERE cat_id IN ($cat_ids)");
$rsAssociations  = mysql_query($query_rsAssociations , $Remoteiran79) or die(mysql_error());
//$row_rsAssociations  = mysql_fetch_array($rsAssociations);
$totalRows_rsAssociations  = mysql_num_rows($rsAssociations);
            
while ($associations = mysql_fetch_assoc($rsAssociations)) {
     $sources[] = $associations{"source_id"};
      
}

                  // create an array of the sources needed
                  $sources[] = $rsAssociations[source_id];      
            }
            if (empty($sources)) {
                  $sources = "0";       
            }
            else {                  
                  $sources = implode(", ", $sources);                  
            }
            return $sources;       
echo ($sources);

Did I apply the changes corectly and if so the echo ($sources); should not show what is in the array?
0
 
LVL 8

Accepted Solution

by:
ropenner earned 500 total points
ID: 34958038
You left in the extra brackets and second call to $sources[] = ....... it should be


Function get_associations($cat_ids) {
	$db = mysql_select_db($database_Remoteiran79, $Remoteiran79);
	$query_associations = ("SELECT * FROM rss_associations WHERE cat_id IN ($cat_ids)");
	$rsAssociations  = mysql_query($query_rsAssociations , $Remoteiran79) or die(mysql_error());
	$totalRows_rsAssociations  = mysql_num_rows($rsAssociations);
	            
	while ($associations = mysql_fetch_assoc($rsAssociations)) {
		$sources[] = $associations{"source_id"};
	}
	if (empty($sources)) { 
		$sources = "0";       
	} else {                  
		$sources = implode(", ", $sources);                  
	}
	echo ($sources);
	return $sources;       
}

Open in new window

0
 

Author Closing Comment

by:babak62
ID: 34958046
Thanks a LOT for having so much patience with me and teach me this well, I really apprecite your kindness.
Million Thanks
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question