Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Storing query results in array

Posted on 2011-02-22
7
Medium Priority
?
227 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
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
By, Vadim Tkachenko. In this article we’ll look at ClickHouse on its one year anniversary.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

963 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