RE: [PHP] splitting up an array into lines ...
The problem is that this line: $rpm_list = `rpm -qa`; gives back a string, so use this: $rpm_list = `rpm -qa`; $rpm_list = explode("\n", $rpm_list); and see how that goes Martin -Original Message- From: Neil Mooney [mailto:[EMAIL PROTECTED]] Sent: Wednesday, January 16, 2002 12:04 AM To: [EMAIL PROTECTED] Subject: [PHP] splitting up an array into lines ... 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`; // open a db connection and insert them into the db include "db.php"; print "working on host : $host\n"; $test = mysql_query ("SELECT * FROM machine_info WHERE host = '$host'"); $test1 = mysql_fetch_object ($test); print "TEST: $test1->host\n"; if ($test1->host == "") { print "machine doesnt exists in the db , adding an entry for $host\n"; $add_machine_to_table = mysql_query("INSERT INTO machine_info (host) VALUES ('$host')"); } // get the rpm list a line at a time 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 Warning: Invalid argument supplied for foreach() in get_rpm_info.php on line 29 -- many thanx in advance Neil :) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] splitting up an array into lines ...
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 > > > Warning: Invalid argument supplied for foreach() in > get_rpm_info.php on line 29 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. */ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
[PHP] splitting up an array into lines ...
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`; // open a db connection and insert them into the db include "db.php"; print "working on host : $host\n"; $test = mysql_query ("SELECT * FROM machine_info WHERE host = '$host'"); $test1 = mysql_fetch_object ($test); print "TEST: $test1->host\n"; if ($test1->host == "") { print "machine doesnt exists in the db , adding an entry for $host\n"; $add_machine_to_table = mysql_query("INSERT INTO machine_info (host) VALUES ('$host')"); } // get the rpm list a line at a time 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 Warning: Invalid argument supplied for foreach() in get_rpm_info.php on line 29 -- many thanx in advance Neil :) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]