Re: Regarding split text and match from data base [message #183914 is a reply to message #183913] |
Mon, 25 November 2013 07:36 |
Arno Welzel
Messages: 317 Registered: October 2011
Karma:
|
Senior Member |
|
|
Arno Welzel, 2013-11-25 08:35:
> jalaf28(at)gmail(dot)com, 2013-11-25 07:35:
>
>> its new solution it works
>> but it skip some computers
>>
>>
>> <?php
>>
>> $out=shell_exec("arp -a");
>
> Can you post an example of the output of arp -a on your system? Maybe
> using preg_match_all() as suggested by Jerry would be the easier way.
> Also keep in mind, that arp -a may output more than one line. Maybe this
> is the reason, why some computers are skipped - they produce multiple
> lines of output, so a simple explode() is not enough.
>
>> $ex=explode(" ",$out);
>>
>> $j=21;
>> $k=32;
>
> I would prefer:
>
> $ip_address = $out[21];
> $mac_address = $out[32];
>
> This makes the remaining code easier to read.
>
>> $ln=mysql_connect("localhost","root","");
>>
>> if(!$ln)
>> {
>> die("Could not Connect to The Server");
>> }
>
> Shorter - as you already did:
>
> $ln = @mysql_connect("localhost", "root", "") or
> die("Could not connect to the server");
>
> Explanation:
>
> @ in front of the mysql_connect() suppresses any error output by the PHP
> runtime.
>
> The construction "function() or die()" will call die() if the function
> does return false, since "or" will try to evaluate the second expression
> (die()) if the first one (function()) will return false.
>
> In production systems you should also keep in mind that using "root" is
> usually not a good idea. It's better to create a separate user who only
> has access to the data you need.
>
>> mysql_select_db("mysql") or die("Could Not Connect with database");
>
> You see ;-)
>
>> $rs=mysql_query("select * from mac_add");
>
> Since you only want to get the data for one specific MAC address anway -
> assuming your table field is named "mac":
>
> $query = "select * from mac_add where mac='" .
> mysql_real_escape_string($mac_address) .
> "'";
> $rs = mysql_query($query);
>
>> echo "<center><table border=1><tr><th>Ip</th><th>System Name</th></tr>";
>
> Now you only have one fetch and no loop. Using htmlspecialchars() makes
> sure, that the output will not be invalid HTML - but if you are sure,
> that you will never have characters like "&", "<", ">" etc. in your
> data, you don't need to use it.
>
> if( $rr = mysql_fetch_array($rs) )
Ooops - this should be:
if( $res = mysql_fetch_array($rs) )
> {
> echo "<tr>" .
> "<td>" . htmlspecialchars($ip_address) . "</td>" .
> "<td>" . htmlspecialchars($res[0]) . "</td>" .
> "<td>" . htmlspecialchars($res[1]) . "</td>" .
> "</tr>";
> }
> echo "</table>";
> mysql_close();
--
Arno Welzel
http://arnowelzel.de
http://de-rec-fahrrad.de
|
|
|