Ronny
Ronny

Reputation: 285

empty value of parameter - php

I have this code:

foreach($summary as $machine)
{
    $hostname = $machine['node'];

    $result = mysql_query("SELECT OS FROM machines WHERE ind='$hostname'");
   while($row = mysql_fetch_array($result))

    {

        if($row == 'solaris')
        {
            $partition_os = 'export/home';
        }
        else
        {
             $partition_os = '/home';
        }
    }
}
  <partition<?php echo $i; ?>><?php echo $partition_os; ?></partition<?php echo $i; ?>>

The output of the query is:(without the where)

mysql> SELECT OS FROM machines;
+---------+
| OS      |
+---------+
| NULL    |
| solaris |
+---------+

My problem is that in my xml (this is for ajax) i see only /home/ instead of export/home. The $hostname supposed to be fine because i use it before.

Thank you!

Upvotes: 0

Views: 110

Answers (4)

codaddict
codaddict

Reputation: 455332

Try brining your echo statement inside the foreach loop as:

foreach($summary as $machine) {

        $hostname = $machine['node'];

        $result = mysql_query("SELECT OS FROM machines WHERE ind='$hostname'");
        while($row = mysql_fetch_array($result)) {

                if($row == 'solaris') {
                        $partition_os = 'export/home';
                } else {
                        $partition_os = '/home';
                }
        }
        echo "<partition$i>".$partition_os."</partition$i>";
}

I don't see what $i is.

Upvotes: 1

grossvogel
grossvogel

Reputation: 6782

It's hard to say whether you're missing some <?php tags for brevity or what, but it seems that your XML is outside your loop. This means you're only printing it once (after the whole loop is finished), using the last value of $partition_os set in the loop.

Based on your comment on another answer (where you did print_r), it looks like your query is returning two rows, the 'solaris' one you're looking for, and then an empty one. It's the latter (empty) one that's setting your $partition_os value.

(...and both other answers are correct that you need to compare $row[0] or $row['OS'])

Upvotes: 0

Sarfraz
Sarfraz

Reputation: 382826

Replace this:

if($row == 'solaris')

With:

if($row['OS'] == 'solaris')

Upvotes: 2

Colin Hebert
Colin Hebert

Reputation: 93187

mysql_fetch_array() returns an array, you're comparing an array to a String here.

Try with $row[0] == 'solaris' to compare.

Upvotes: 2

Related Questions