Solved

Storing query results in array

Posted on 2011-02-22
7
220 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:Dushan911
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
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
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

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This article discusses four methods for overlaying images in a container on a web page
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.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

914 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

13 Experts available now in Live!

Get 1:1 Help Now