Solved

Storing query results in array

Posted on 2011-02-22
7
224 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
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.

729 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