Solved

Storing query results in array

Posted on 2011-02-22
7
219 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
Comment Utility
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:Dushan911
Comment Utility
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
Comment Utility
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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 8

Expert Comment

by:ropenner
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
how can I dected if URL has variable? 7 21
Link SQL table to Webpage 9 32
Not needed 13 52
Insert with SET how to handle join 6 25
As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

763 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now