-3

I have extracted some data from mysql database using php. In the script, i have

$result=mysql_query("select mac,ip_add from arp_table where ip_add='$var'");
$row=mysql_fetch_row($result);
print_r($row);

The result comes out as

Array ( [0] => 08 CC 68 71 A1 C0 [1] => 1.10.0.0.98 )

But i would want to modify it and have it come out as

MAC Address for 1.10.0.0.98 is 08 CC 68 71 A1 C0 How can i achieve this result?

6 Answers 6

3

Replace

print_r($row);

with

echo "MAC Address for {$row[1]} is {$row[0]}"

Please, also note that using variables inside strings only works with double quotes strings. The curly braces are only needed for particular variable names, or when you want to use array accessors.

see this question for more information about concatenation and single vs double quoted strings.

2
select 'MAC Address for ' + ip_add + ' is ' + mac
from arp_table 
where ip_add='$var'
1

just do it with

if(!empty($row)) {

   echo "MAC Address for ".$row[1]." is ".$row[0];

}

hope this will sure help you

1
function printArpRow($row) {
        echo "MAC Address for {$row[0]} is {$row[0]}";
    }

Then change print_r($row) by printArpRow($row)

1

Using your actual code, change print_r($row); for:

echo "MAC Address for ".$row[0]." is ".$row[1];
0

Try

$result=mysql_query("select concat('MAC Address for',mac,'is',ip_add) as str from arp_table where ip_add='$var'");
$row=mysql_fetch_row($result);
print_r($row);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.