Re: splitting up an array into lines ...

From: Date: Tue, 15 Jan 2002 13:29:05 +0000
Subject: Re: splitting up an array into lines ...
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-80558@lists.php.net to get a copy of this message
On Tuesday 15 January 2002 21:04, Neil Mooney wrote: > I want this code to work thru a large cluster and put the rpm information > into a db , > it all works apart from the foreach loop. > > why doesnt my code work ( in particular the foreach loop ) , > > // get hostname > $host = hostname; > > // get a list of rpms > > $rpm_list = rpm -qa; [snip] > foreach($rpm_list as $rpm) > { > > $query = "UPDATE machine_info SET rpm = '$rpm' WHERE host = > '$host'"; > > if (!(mysql_query($query))) > { > print "Mysql could not do the update query - for host > $host"; > } > } > > ------------ > > i get : > > X-Powered-By: PHP/4.0.6 > Content-type: text/html > > > working on host : lxplus038 > > TEST: lxplus038 > > <br> > <b>Warning</b>: Invalid argument supplied for foreach() in <b> > get_rpm_info.php</b> on line <b>29</b><br> That's telling you that $rpm_list is not an array. The output of rpm -qa is just a string (with \n for newlines). Thus to change $rpm_list into an array just do: $rpm_list = explode("\n", $rpm_list); hth -- Jason Wong -> Gremlins Associates -> www.gremlins.com.hk /* Alden's Laws: (1) Giving away baby clothes and furniture is the major cause of pregnancy. (2) Always be backlit. (3) Sit down whenever possible. */

« previous php.general (#80558) next »