FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » Regarding split text and match from data base
Show: Today's Messages :: Unread Messages :: Show Polls :: Message Navigator
| Subscribe to topic | Bookmark topic 
Switch to threaded view of this topic Create a new topic Submit Reply
Regarding split text and match from data base [message #183843] Fri, 22 November 2013 06:28 Go to next message
jalaf28 is currently offline  jalaf28
Messages: 8
Registered: November 2013
Karma: 0
Junior Member
add to buddy list
ignore all messages by this user
Hello All
I just start learning PHP and i am using 5.4.16 i just want to split the out put of the following and match from the database and print the System name.

here is the Code

<?php

ob_start();
$line=shell_exec("arp -a");
echo "<pre>;
echo $line;
echo "</pre>";

?>

It shows the output of the command "arp -a" and show the Ip address and Mac address on the LAN system. i saved the name and the Mac address in the database.
now i want to extaxct the command output and match ["MAc address] with database and show the Particular system name and Ip Address. i try google but not sufficent.

i just want to show th eout put like


IP Name
192.168.1.x Server1
192.168.1.x Client1

Thanks
Amit
Re: Regarding split text and match from data base [message #183850 is a reply to message #183843] Fri, 22 November 2013 09:24 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
add to buddy list
ignore all messages by this user
On 11/22/2013 6:28 AM, jalaf28(at)gmail(dot)com wrote:
> Hello All
> I just start learning PHP and i am using 5.4.16 i just want to split the out put of the following and match from the database and print the System name.
>
> here is the Code
>
> <?php
>
> ob_start();
> $line=shell_exec("arp -a");
> echo "<pre>;
> echo $line;
> echo "</pre>";
>
> ?>
>
> It shows the output of the command "arp -a" and show the Ip address and Mac address on the LAN system. i saved the name and the Mac address in the database.
> now i want to extaxct the command output and match ["MAc address] with database and show the Particular system name and Ip Address. i try google but not sufficent.
>
> i just want to show th eout put like
>
>
> IP Name
> 192.168.1.x Server1
> 192.168.1.x Client1
>
> Thanks
> Amit
>
>
>

Well, you will need to parse the output from your arp command to get the
list if IP addresses; a regex function such as preg_match_all will help
you here.

Then you take the output array and use the matching IP addresses to
search your database. The function calls will depend on the database
you're using.

You probably won't find anything like this on the internet; it's not the
type of thing that's commonly done (although there isn't any problem
with it).

BTW - why are you using ob_start()? It just adds overhead to the
process. There are very few reasons you ever need to use it.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: Regarding split text and match from data base [message #183854 is a reply to message #183843] Fri, 22 November 2013 10:34 Go to previous messageGo to next message
Arno Welzel is currently offline  Arno Welzel
Messages: 317
Registered: October 2011
Karma: 0
Senior Member
add to buddy list
ignore all messages by this user
Am 22.11.2013 12:28, schrieb jalaf28(at)gmail(dot)com:

> Hello All I just start learning PHP and i am using 5.4.16 i just
> want to split the out put of the following and match from the
> database and print the System name.
>
> here is the Code
>
> <?php
>
> ob_start(); $line=shell_exec("arp -a"); echo "<pre>; echo $line; echo
> "</pre>";
>
> ?>
>
> It shows the output of the command "arp -a" and show the Ip address
> and Mac address on the LAN system. i saved the name and the Mac
> address in the database. now i want to extaxct the command output and
> match ["MAc address] with database and show the Particular system
> name and Ip Address. i try google but not sufficent.

You can try explode() using space (' ') as delimiter, see:

<http://php.net/manual/en/function.explode.php>



--
Arno Welzel
http://arnowelzel.de
http://de-rec-fahrrad.de
Re: Regarding split text and match from data base [message #183856 is a reply to message #183854] Fri, 22 November 2013 10:54 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
add to buddy list
ignore all messages by this user
On 11/22/2013 10:34 AM, Arno Welzel wrote:
> Am 22.11.2013 12:28, schrieb jalaf28(at)gmail(dot)com:
>
>> Hello All I just start learning PHP and i am using 5.4.16 i just
>> want to split the out put of the following and match from the
>> database and print the System name.
>>
>> here is the Code
>>
>> <?php
>>
>> ob_start(); $line=shell_exec("arp -a"); echo "<pre>; echo $line; echo
>> "</pre>";
>>
>> ?>
>>
>> It shows the output of the command "arp -a" and show the Ip address
>> and Mac address on the LAN system. i saved the name and the Mac
>> address in the database. now i want to extaxct the command output and
>> match ["MAc address] with database and show the Particular system
>> name and Ip Address. i try google but not sufficent.
>
> You can try explode() using space (' ') as delimiter, see:
>
> <http://php.net/manual/en/function.explode.php>
>
>
>

Arno,

Have you actually looked at the output of arp -a? From your response, I
don't think so.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: Regarding split text and match from data base [message #183858 is a reply to message #183856] Fri, 22 November 2013 11:50 Go to previous messageGo to next message
Arno Welzel is currently offline  Arno Welzel
Messages: 317
Registered: October 2011
Karma: 0
Senior Member
add to buddy list
ignore all messages by this user
Jerry Stuckle, 2013-11-22 16:54:

> On 11/22/2013 10:34 AM, Arno Welzel wrote:
>> Am 22.11.2013 12:28, schrieb jalaf28(at)gmail(dot)com:
>>
>>> Hello All I just start learning PHP and i am using 5.4.16 i just
>>> want to split the out put of the following and match from the
>>> database and print the System name.
>>>
>>> here is the Code
>>>
>>> <?php
>>>
>>> ob_start(); $line=shell_exec("arp -a"); echo "<pre>; echo $line; echo
>>> "</pre>";
>>>
>>> ?>
>>>
>>> It shows the output of the command "arp -a" and show the Ip address
>>> and Mac address on the LAN system. i saved the name and the Mac
>>> address in the database. now i want to extaxct the command output and
>>> match ["MAc address] with database and show the Particular system
>>> name and Ip Address. i try google but not sufficent.
>>
>> You can try explode() using space (' ') as delimiter, see:
>>
>> <http://php.net/manual/en/function.explode.php>
>>
>>
>>
>
> Arno,
>
> Have you actually looked at the output of arp -a? From your response, I
> don't think so.

And where is your answer to the question?

Anyway...

arp -a outputs here (numbers as "x" for privacy reasons):

somename (xxx.xxx.xxx.xxx) at xx:xx:xx:xx:xx:xx [ether] on eth0

So - using explode() on this line with space as delimiter should return
an array with the following elements:

somename
(xxx.xxx.xxx.xxx)
at
xx:xx:xx:xx:xx:xx
[ether]
on
eth0

Then you can use the fourth element which contains the MAC address.

If you get more than one line of output, the lines have to be split
first of course. Therefore I'd recommend using exec() which will return
the output as an array with one element for each line of output.



--
Arno Welzel
http://arnowelzel.de
http://de-rec-fahrrad.de
Re: Regarding split text and match from data base [message #183861 is a reply to message #183858] Fri, 22 November 2013 13:57 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
add to buddy list
ignore all messages by this user
On 11/22/2013 11:50 AM, Arno Welzel wrote:
> Jerry Stuckle, 2013-11-22 16:54:
>
>> On 11/22/2013 10:34 AM, Arno Welzel wrote:
>>> Am 22.11.2013 12:28, schrieb jalaf28(at)gmail(dot)com:
>>>
>>>> Hello All I just start learning PHP and i am using 5.4.16 i just
>>>> want to split the out put of the following and match from the
>>>> database and print the System name.
>>>>
>>>> here is the Code
>>>>
>>>> <?php
>>>>
>>>> ob_start(); $line=shell_exec("arp -a"); echo "<pre>; echo $line; echo
>>>> "</pre>";
>>>>
>>>> ?>
>>>>
>>>> It shows the output of the command "arp -a" and show the Ip address
>>>> and Mac address on the LAN system. i saved the name and the Mac
>>>> address in the database. now i want to extaxct the command output and
>>>> match ["MAc address] with database and show the Particular system
>>>> name and Ip Address. i try google but not sufficent.
>>>
>>> You can try explode() using space (' ') as delimiter, see:
>>>
>>> <http://php.net/manual/en/function.explode.php>
>>>
>>>
>>>
>>
>> Arno,
>>
>> Have you actually looked at the output of arp -a? From your response, I
>> don't think so.
>
> And where is your answer to the question?
>
> Anyway...
>
> arp -a outputs here (numbers as "x" for privacy reasons):
>
> somename (xxx.xxx.xxx.xxx) at xx:xx:xx:xx:xx:xx [ether] on eth0
>
> So - using explode() on this line with space as delimiter should return
> an array with the following elements:
>
> somename
> (xxx.xxx.xxx.xxx)
> at
> xx:xx:xx:xx:xx:xx
> [ether]
> on
> eth0
>
> Then you can use the fourth element which contains the MAC address.
>
> If you get more than one line of output, the lines have to be split
> first of course. Therefore I'd recommend using exec() which will return
> the output as an array with one element for each line of output.
>
>
>

My reply is above - if you bothered to read (which you obviously
didn't). Additionally, the output of arp -a is also dependent on the
operating system and version of arp. Your way requires much more
processing than a simple preg_match().

And in this case the regex is pretty simple, and parses the output he
wants into a multi-dimensional array in one statement.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: Regarding split text and match from data base [message #183872 is a reply to message #183861] Sat, 23 November 2013 02:52 Go to previous messageGo to next message
Arno Welzel is currently offline  Arno Welzel
Messages: 317
Registered: October 2011
Karma: 0
Senior Member
add to buddy list
ignore all messages by this user
Jerry Stuckle, 2013-11-22 19:57:

> On 11/22/2013 11:50 AM, Arno Welzel wrote:
>> Jerry Stuckle, 2013-11-22 16:54:
>>
>>> On 11/22/2013 10:34 AM, Arno Welzel wrote:
>>>> Am 22.11.2013 12:28, schrieb jalaf28(at)gmail(dot)com:
>>>>
>>>> > Hello All I just start learning PHP and i am using 5.4.16 i just
>>>> > want to split the out put of the following and match from the
>>>> > database and print the System name.
>>>> >
>>>> > here is the Code
>>>> >
>>>> > <?php
>>>> >
>>>> > ob_start(); $line=shell_exec("arp -a"); echo "<pre>; echo $line; echo
>>>> > "</pre>";
>>>> >
>>>> > ?>
>>>> >
>>>> > It shows the output of the command "arp -a" and show the Ip address
>>>> > and Mac address on the LAN system. i saved the name and the Mac
>>>> > address in the database. now i want to extaxct the command output and
>>>> > match ["MAc address] with database and show the Particular system
>>>> > name and Ip Address. i try google but not sufficent.
>>>>
>>>> You can try explode() using space (' ') as delimiter, see:
>>>>
>>>> <http://php.net/manual/en/function.explode.php>
>>>>
>>>>
>>>>
>>>
>>> Arno,
>>>
>>> Have you actually looked at the output of arp -a? From your response, I
>>> don't think so.
>>
>> And where is your answer to the question?
>>
>> Anyway...
>>
>> arp -a outputs here (numbers as "x" for privacy reasons):
>>
>> somename (xxx.xxx.xxx.xxx) at xx:xx:xx:xx:xx:xx [ether] on eth0
>>
>> So - using explode() on this line with space as delimiter should return
>> an array with the following elements:
>>
>> somename
>> (xxx.xxx.xxx.xxx)
>> at
>> xx:xx:xx:xx:xx:xx
>> [ether]
>> on
>> eth0
>>
>> Then you can use the fourth element which contains the MAC address.
>>
>> If you get more than one line of output, the lines have to be split
>> first of course. Therefore I'd recommend using exec() which will return
>> the output as an array with one element for each line of output.
>>
>>
>>
>
> My reply is above - if you bothered to read (which you obviously
> didn't). Additionally, the output of arp -a is also dependent on the
> operating system and version of arp. Your way requires much more
> processing than a simple preg_match().

Yep - I didn't see your other post when I wrote my reply.

And yes, I'm also aware that the output of arp -a depends on the
operating system version. That's why I said - "You can try explode()"
and not "use explode() this will always work". Of course I haven taken
into account that the output format is not the same on all operating
systems.

> And in this case the regex is pretty simple, and parses the output he
> wants into a multi-dimensional array in one statement.

Sury - regex is pretty simple assuming the OP knows how to use regular
expressions ;-)

But even then additional parsing may be neccessary if arp groups
interfaces and outputs the interface description first and then a list
of entries for that interface like Windows 7 does.

Again - that's why I said "You can try explode()" - it was only a
suggestion, not a complete solution. But preg_metch() is neither.


--
Arno Welzel
http://arnowelzel.de
http://de-rec-fahrrad.de
Re: Regarding split text and match from data base [message #183874 is a reply to message #183872] Sat, 23 November 2013 07:05 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
add to buddy list
ignore all messages by this user
On 11/23/2013 2:52 AM, Arno Welzel wrote:
> Jerry Stuckle, 2013-11-22 19:57:
>
>> On 11/22/2013 11:50 AM, Arno Welzel wrote:
>>> Jerry Stuckle, 2013-11-22 16:54:
>>>
>>>> On 11/22/2013 10:34 AM, Arno Welzel wrote:
>>>> > Am 22.11.2013 12:28, schrieb jalaf28(at)gmail(dot)com:
>>>> >
>>>> >> Hello All I just start learning PHP and i am using 5.4.16 i just
>>>> >> want to split the out put of the following and match from the
>>>> >> database and print the System name.
>>>> >>
>>>> >> here is the Code
>>>> >>
>>>> >> <?php
>>>> >>
>>>> >> ob_start(); $line=shell_exec("arp -a"); echo "<pre>; echo $line; echo
>>>> >> "</pre>";
>>>> >>
>>>> >> ?>
>>>> >>
>>>> >> It shows the output of the command "arp -a" and show the Ip address
>>>> >> and Mac address on the LAN system. i saved the name and the Mac
>>>> >> address in the database. now i want to extaxct the command output and
>>>> >> match ["MAc address] with database and show the Particular system
>>>> >> name and Ip Address. i try google but not sufficent.
>>>> >
>>>> > You can try explode() using space (' ') as delimiter, see:
>>>> >
>>>> > <http://php.net/manual/en/function.explode.php>
>>>> >
>>>> >
>>>> >
>>>>
>>>> Arno,
>>>>
>>>> Have you actually looked at the output of arp -a? From your response, I
>>>> don't think so.
>>>
>>> And where is your answer to the question?
>>>
>>> Anyway...
>>>
>>> arp -a outputs here (numbers as "x" for privacy reasons):
>>>
>>> somename (xxx.xxx.xxx.xxx) at xx:xx:xx:xx:xx:xx [ether] on eth0
>>>
>>> So - using explode() on this line with space as delimiter should return
>>> an array with the following elements:
>>>
>>> somename
>>> (xxx.xxx.xxx.xxx)
>>> at
>>> xx:xx:xx:xx:xx:xx
>>> [ether]
>>> on
>>> eth0
>>>
>>> Then you can use the fourth element which contains the MAC address.
>>>
>>> If you get more than one line of output, the lines have to be split
>>> first of course. Therefore I'd recommend using exec() which will return
>>> the output as an array with one element for each line of output.
>>>
>>>
>>>
>>
>> My reply is above - if you bothered to read (which you obviously
>> didn't). Additionally, the output of arp -a is also dependent on the
>> operating system and version of arp. Your way requires much more
>> processing than a simple preg_match().
>
> Yep - I didn't see your other post when I wrote my reply.
>

Gee, it was only an hour before your post.

> And yes, I'm also aware that the output of arp -a depends on the
> operating system version. That's why I said - "You can try explode()"
> and not "use explode() this will always work". Of course I haven taken
> into account that the output format is not the same on all operating
> systems.
>

Then why tell him something which may not work?

>> And in this case the regex is pretty simple, and parses the output he
>> wants into a multi-dimensional array in one statement.
>
> Sury - regex is pretty simple assuming the OP knows how to use regular
> expressions ;-)
>

There is nothing complicated about his regex. A little reading of the
tutorial and one of the regex test engines should get him by very
easily. And if he still has problems, posting the output of arp -a here
and people will help him.

> But even then additional parsing may be neccessary if arp groups
> interfaces and outputs the interface description first and then a list
> of entries for that interface like Windows 7 does.
>

Not if it's done right. It would be more complicated, but a regex could
be built to process Windows output, also.

> Again - that's why I said "You can try explode()" - it was only a
> suggestion, not a complete solution. But preg_metch() is neither.
>
>

Shows how little you understand preg_match_all() (not preg_match()).

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: Regarding split text and match from data base [message #183885 is a reply to message #183874] Sun, 24 November 2013 11:17 Go to previous messageGo to next message
Arno Welzel is currently offline  Arno Welzel
Messages: 317
Registered: October 2011
Karma: 0
Senior Member
add to buddy list
ignore all messages by this user
Jerry Stuckle, 2013-11-23 13:05:

> On 11/23/2013 2:52 AM, Arno Welzel wrote:
[...]
>> Again - that's why I said "You can try explode()" - it was only a
>> suggestion, not a complete solution. But preg_metch() is neither.
>>
>>
>
> Shows how little you understand preg_match_all() (not preg_match()).

Oh - I have a typo in my post due to leaving out "_all" and you assume I
don't understand preg_match_all() - what a brilliant logic ;-)

Well - if it makes you happy, than think so and just ignore my posts.


--
Arno Welzel
http://arnowelzel.de
http://de-rec-fahrrad.de
Re: Regarding split text and match from data base [message #183886 is a reply to message #183885] Sun, 24 November 2013 11:23 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
add to buddy list
ignore all messages by this user
On 11/24/2013 11:17 AM, Arno Welzel wrote:
> Jerry Stuckle, 2013-11-23 13:05:
>
>> On 11/23/2013 2:52 AM, Arno Welzel wrote:
> [...]
>>> Again - that's why I said "You can try explode()" - it was only a
>>> suggestion, not a complete solution. But preg_metch() is neither.
>>>
>>>
>>
>> Shows how little you understand preg_match_all() (not preg_match()).
>
> Oh - I have a typo in my post due to leaving out "_all" and you assume I
> don't understand preg_match_all() - what a brilliant logic ;-)
>
> Well - if it makes you happy, than think so and just ignore my posts.
>
>

No, it wasn't a typo. It was your usual show of ignorance.

But then trolls always try to blame their stoopidity on someone else.


--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: Regarding split text and match from data base [message #183907 is a reply to message #183843] Sun, 24 November 2013 23:02 Go to previous messageGo to next message
jalaf28 is currently offline  jalaf28
Messages: 8
Registered: November 2013
Karma: 0
Junior Member
add to buddy list
ignore all messages by this user
Thanks For all of you for the reply.
Re: Regarding split text and match from data base [message #183909 is a reply to message #183843] Mon, 25 November 2013 01:09 Go to previous messageGo to next message
jalaf28 is currently offline  jalaf28
Messages: 8
Registered: November 2013
Karma: 0
Junior Member
add to buddy list
ignore all messages by this user
i found the solutio.but its not work properly. because it prints the Ip and System name but no correct.

here is the code what should i do?

<?php

$out=shell_exec("arp -a");



$ex=explode(" ",$out);

$j=21; // variable used for Ip address
$k=32; // varibale used for MAC Address

$ln=mysql_connect("localhost","root","");

if(!$ln)
{
die("Could not Connect to The Server");
}
mysql_select_db("mysql") or die("Could Not Connect with database");

$rs=mysql_query("select * from mac_add");
echo "<center><table border=1><tr><th>Ip</th><th>System Name</th></tr>";
while($rr=mysql_fetch_array($rs))
{
if($rr[0] == $ex[$k])
{
//echo $ex[$j] . " " . $rr[0] . "<br />";

echo "<tr><td>" .$ex[$j] . "</td><td>" . $rr[0] . "</td> </tr>";
}
$j +=21;
$k +=21;


}


echo "</table></center>";
?>



its not matching [Comapare with the MAc in the Database]

what should i Do ?
Re: Regarding split text and match from data base [message #183911 is a reply to message #183843] Mon, 25 November 2013 01:35 Go to previous messageGo to next message
jalaf28 is currently offline  jalaf28
Messages: 8
Registered: November 2013
Karma: 0
Junior Member
add to buddy list
ignore all messages by this user
its new solution it works
but it skip some computers


<?php

$out=shell_exec("arp -a");



$ex=explode(" ",$out);

$j=21;
$k=32;

$ln=mysql_connect("localhost","root","");

if(!$ln)
{
die("Could not Connect to The Server");
}
mysql_select_db("mysql") or die("Could Not Connect with database");

$rs=mysql_query("select * from mac_add");
echo "<center><table border=1><tr><th>Ip</th><th>System Name</th></tr>";
while($rr=mysql_fetch_array($rs))
{
$rsr=mysql_query("select * from mac_add where mac_id= '$ex[$k]'");
while( $res=mysql_fetch_array($rsr))
{
/* if($rr[0] == $ex[$k])
{
//echo $ex[$j] . " " . $rr[0] . "<br />";
*/
echo "<tr><td>" .$ex[$j] . "</td><td>" . $res[0] . "</td> </td><td>" .$res[1] . "</td></tr>" ;
}
$j +=21;
$k +=21;


}


echo "</table></center>";

mysql_close();
?>
Re: Regarding split text and match from data base [message #183913 is a reply to message #183911] Mon, 25 November 2013 02:35 Go to previous messageGo to next message
Arno Welzel is currently offline  Arno Welzel
Messages: 317
Registered: October 2011
Karma: 0
Senior Member
add to buddy list
ignore all messages by this user
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) )
{
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
Re: Regarding split text and match from data base [message #183914 is a reply to message #183913] Mon, 25 November 2013 02:36 Go to previous messageGo to next message
Arno Welzel is currently offline  Arno Welzel
Messages: 317
Registered: October 2011
Karma: 0
Senior Member
add to buddy list
ignore all messages by this user
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
Re: Regarding split text and match from data base [message #183916 is a reply to message #183911] Mon, 25 November 2013 09:31 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
add to buddy list
ignore all messages by this user
On 11/25/2013 1:35 AM, jalaf28(at)gmail(dot)com wrote:
> its new solution it works
> but it skip some computers
>
>
> <?php
>
> $out=shell_exec("arp -a");
>
>
>
> $ex=explode(" ",$out);
>
> $j=21;
> $k=32;
>
> $ln=mysql_connect("localhost","root","");
>
> if(!$ln)
> {
> die("Could not Connect to The Server");
> }

It's almost never right to use die() like this in a script. It
terminates all processing of the page immediately, causing invalid HTML
to be generated. Better is to put out a message and not process any
code which relies on the connection, but allows the rest of the code and
HTML to complete normally.

> mysql_select_db("mysql") or die("Could Not Connect with database");
>
> $rs=mysql_query("select * from mac_add");

What is the purpose of this statement? Why do you want to fetch the
entire table?

> echo "<center><table border=1><tr><th>Ip</th><th>System Name</th></tr>";
> while($rr=mysql_fetch_array($rs))

I'm not sure why you want to go through the entire database here.

> {
> $rsr=mysql_query("select * from mac_add where mac_id= '$ex[$k]'");

"select *" is also generally not a good idea. It is better to specify
the exact columns you want. That way if you change the database later,
it will cause fewer problems. For instance, if you add a large BLOB
column, you won't be retrieving a lot of data you aren't using. Also,
if you DROP a column or change the columns name, the error will show up
on the SELECT statement and not later.

> while( $res=mysql_fetch_array($rsr))

I prefer mysql_fetch_assoc(). It is clearer because you identify the
columns by name and not by index, helping to self-document the code.

> {
> /* if($rr[0] == $ex[$k])
> {
> //echo $ex[$j] . " " . $rr[0] . "<br />";
> */
> echo "<tr><td>" .$ex[$j] . "</td><td>" . $res[0] . "</td> </td><td>" .$res[1] . "</td></tr>" ;
> }
> $j +=21;
> $k +=21;
>
>
> }
>
>
> echo "</table></center>";
>
> mysql_close();
> ?>
>

A couple of other things. First of all, the mysql_xxx functions are
deprecated and will eventually disappear. You should use the newer
mysqli_xxx() functions. Most are similar to the mysql_xxx() functions,
but mysqli_xxx() also provides additional options.

Also, you can get all of the MAC addresses in your arp table at once
with one query, i.e.

SELECT x,y.z
FROM table
WHERE value in ('val1', 'val2', val3');

It will take a bit of work to build the list of mac addresses, so it's
probably something you should wait for later. But it will be much more
efficient, as you'll only make one call to the database and allow it to
do the searching for you.

But the main question here is - what is the output of your arp -a command?

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: Regarding split text and match from data base [message #183929 is a reply to message #183843] Tue, 26 November 2013 00:32 Go to previous messageGo to next message
jalaf28 is currently offline  jalaf28
Messages: 8
Registered: November 2013
Karma: 0
Junior Member
add to buddy list
ignore all messages by this user
Thanks All

It works i apply your Suggestions

and i use this ARP command on window's 7

the output of the command "arp -a" is

Interface: 192.168.x.x --- 0xb
Internet Address Physical Address Type
192.168.x.x XX-XX-XX-XX-XX-XX dynamic
192.168.x.x XX-XX-XX-XX-XX-XX dynamic
192.168.x.x XX-XX-XX-XX-XX-XX dynamic
192.168.x.x XX-XX-XX-XX-XX-XX dynamic
192.168.x.x XX-XX-XX-XX-XX-XX dynamic
192.168.x.xa XX-XX-XX-XX-XX-XX dynamic
192.168.x.xxx XX-XX-XX-XX-XX-XX static


These Are list of Ip And the Mac Address of the Lan Its Daynamic Not Static..And the Mac address and the System names are stored in the database.in that problem it will take the physical addrss [ from arp] and match with the database [mac_id column] and return the System name.

and the Out put of the Programm will be Lie This....

IP Address System Name
192.168.x.x Server1
192.168.x.x Client1
192.168.x.x Client2
192.168.x.x Client3
Re: Regarding split text and match from data base [message #183930 is a reply to message #183843] Tue, 26 November 2013 00:41 Go to previous messageGo to next message
jalaf28 is currently offline  jalaf28
Messages: 8
Registered: November 2013
Karma: 0
Junior Member
add to buddy list
ignore all messages by this user
It works but it show's the Ip and MAc address only IP less then 10.for example

it shows 192.168.xx.9
but after 192.168.xx.11 it not show
Re: Regarding split text and match from data base [message #183931 is a reply to message #183930] Tue, 26 November 2013 07:20 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
add to buddy list
ignore all messages by this user
On 11/26/2013 12:41 AM, jalaf28(at)gmail(dot)com wrote:
> It works but it show's the Ip and MAc address only IP less then 10.for example
>
> it shows 192.168.xx.9
> but after 192.168.xx.11 it not show
>

You don't show your code, so it's impossible for us to help you find
your problem.

One basic rule. If you're having problems with a piece of code, you
need to post the failing code for others to see.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: Regarding split text and match from data base [message #183935 is a reply to message #183931] Wed, 27 November 2013 00:22 Go to previous messageGo to next message
jalaf28 is currently offline  jalaf28
Messages: 8
Registered: November 2013
Karma: 0
Junior Member
add to buddy list
ignore all messages by this user
Hi Jerry

i already Post It
but now i m post it again..

here is the Code..


<?php

$out=shell_exec("arp -a");

$ex=explode(" ",$out);

$j=21;
$k=32;

$ln=@mysql_connect("localhost","root","");

if(!$ln)
{
die("Could not Connect to The Server");
}

mysql_select_db("mysql") or die("Could Not Connect with database");

$rs=mysql_query("select * from mac_add");



while($rr=mysql_fetch_array($rs))
{
$rsr=mysql_query("select * from mac_add where mac_id= '$ex[$k]'");
while( $res=mysql_fetch_array($rsr))
{

echo "<tr><td>" . htmlspecialchars($ex[$j]) . "</td><td>" . htmlspecialchars($res[0]) . "</td> </td><td>" . htmlspecialchars($res[1]) . "</td></tr>" ;

}

$j +=21;
$k +=21;

}


echo "</table></center>";

mysql_close();

?>
Re: Regarding split text and match from data base [message #183937 is a reply to message #183935] Wed, 27 November 2013 06:29 Go to previous messageGo to next message
Arno Welzel is currently offline  Arno Welzel
Messages: 317
Registered: October 2011
Karma: 0
Senior Member
add to buddy list
ignore all messages by this user
Am 27.11.2013 06:22, schrieb jalaf28(at)gmail(dot)com:

> here is the Code..
>
>
> <?php
>
> $out=shell_exec("arp -a");
>
> $ex=explode(" ",$out);
>
> $j=21;
> $k=32;
>
> $ln=@mysql_connect("localhost","root","");
>
> if(!$ln)
> {
> die("Could not Connect to The Server");
> }
>
> mysql_select_db("mysql") or die("Could Not Connect with database");
>
> $rs=mysql_query("select * from mac_add");
>
>
>
> while($rr=mysql_fetch_array($rs))
> {
> $rsr=mysql_query("select * from mac_add where mac_id= '$ex[$k]'");
> while( $res=mysql_fetch_array($rsr))
> {
>
> echo "<tr><td>" . htmlspecialchars($ex[$j]) . "</td><td>" . htmlspecialchars($res[0]) . "</td> </td><td>" . htmlspecialchars($res[1]) . "</td></tr>" ;
>
> }
>
> $j +=21;
> $k +=21;
>
> }

This makes no sense - you first select ALL records which you then don't
use at all.

You also assume that every 21th character a new output line of arp
begins - an very insecure assumption and even more worse you loop
through the number of records in your mac_add table - so you assume
there are never more output lines in arp -a than records in your
database. Therefore if arp will output 15 lines and your table only has
10 records you would always miss 5 lines - since the loop only handles
the 10 records.

If you want to make sure you get a result for every output line from
"arp -a" you should split the lines of the output and loop through that
and not the database records.

This is just a quick & dirty hack and may contain typos - but you may
get the idea:

<?php
$ln=@mysql_connect("localhost","root","");

if(!$ln)
{
// TODO: Gracefully error handling - and not just
// terminating the script
die("Could not Connect to the database");
}

echo "<table>";

// Execute "arp -a" with the output lines in the $output array
// and the return code of the arp command in $returncode
exec("arp -a", $output, $returncode);

// TODO: Check $returncode and handle errors

// Loop through all output lines from the arp command
foreach($output as $line)
{
// TODO: Check, if $line contains an IP/Mac address at all
$elements = explode(" ", $line);

// Build query to look up the entry for a given MAC address
$query = "select * from mac_add where mac_id='".
mysql_real_escape_string($elements[32])."'";

// Execute query
$recordset = mysql_query($query);

// Fetch result and output it if there is one
if($record = mysql_fetch_array($recordset))
{
echo "<tr>" .
"<td>" . htmlspecialchars($elements[21]) . "</td>" .
"<td>" . htmlspecialchars($record[0]) . "</td>" .
"<td>" . htmlspecialchars($record[1]) . "</td>" .
"</tr>";
}
}

echo "</table>";

mysql_close();
?>



--
Arno Welzel
http://arnowelzel.de
http://de-rec-fahrrad.de
Re: Regarding split text and match from data base [message #183938 is a reply to message #183937] Wed, 27 November 2013 06:31 Go to previous messageGo to next message
Arno Welzel is currently offline  Arno Welzel
Messages: 317
Registered: October 2011
Karma: 0
Senior Member
add to buddy list
ignore all messages by this user
Am 27.11.2013 12:29, schrieb Arno Welzel:

> Am 27.11.2013 06:22, schrieb jalaf28(at)gmail(dot)com:
>
[...]
>> $j +=21;
>> $k +=21;
>>
>> }
>
> This makes no sense - you first select ALL records which you then don't
> use at all.
>
> You also assume that every 21th character a new output line of arp

Of course "element", not "character"...

> begins - an very insecure assumption and even more worse you loop
> through the number of records in your mac_add table - so you assume
> there are never more output lines in arp -a than records in your
> database. Therefore if arp will output 15 lines and your table only has
> 10 records you would always miss 5 lines - since the loop only handles
> the 10 records.
>
> If you want to make sure you get a result for every output line from
> "arp -a" you should split the lines of the output and loop through that
> and not the database records.
>
> This is just a quick & dirty hack and may contain typos - but you may
> get the idea:
>
> <?php
> $ln=@mysql_connect("localhost","root","");
>
> if(!$ln)
> {
> // TODO: Gracefully error handling - and not just
> // terminating the script
> die("Could not Connect to the database");
> }
>
> echo "<table>";
>
> // Execute "arp -a" with the output lines in the $output array
> // and the return code of the arp command in $returncode
> exec("arp -a", $output, $returncode);
>
> // TODO: Check $returncode and handle errors
>
> // Loop through all output lines from the arp command
> foreach($output as $line)
> {
> // TODO: Check, if $line contains an IP/Mac address at all
> $elements = explode(" ", $line);
>
> // Build query to look up the entry for a given MAC address
> $query = "select * from mac_add where mac_id='".
> mysql_real_escape_string($elements[32])."'";
>
> // Execute query
> $recordset = mysql_query($query);
>
> // Fetch result and output it if there is one
> if($record = mysql_fetch_array($recordset))
> {
> echo "<tr>" .
> "<td>" . htmlspecialchars($elements[21]) . "</td>" .
> "<td>" . htmlspecialchars($record[0]) . "</td>" .
> "<td>" . htmlspecialchars($record[1]) . "</td>" .
> "</tr>";
> }
> }
>
> echo "</table>";
>
> mysql_close();
> ?>


--
Arno Welzel
http://arnowelzel.de
http://de-rec-fahrrad.de
Re: Regarding split text and match from data base [message #183939 is a reply to message #183935] Wed, 27 November 2013 09:57 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
add to buddy list
ignore all messages by this user
On 11/27/2013 12:22 AM, jalaf28(at)gmail(dot)com wrote:
> Hi Jerry
>
> i already Post It
> but now i m post it again..
>
> here is the Code..
>
>
> <?php
>
> $out=shell_exec("arp -a");
>
> $ex=explode(" ",$out);
>
> $j=21;
> $k=32;
>
> $ln=@mysql_connect("localhost","root","");
>
> if(!$ln)
> {
> die("Could not Connect to The Server");
> }
>
> mysql_select_db("mysql") or die("Could Not Connect with database");
>
> $rs=mysql_query("select * from mac_add");
>
>
>
> while($rr=mysql_fetch_array($rs))
> {
> $rsr=mysql_query("select * from mac_add where mac_id= '$ex[$k]'");
> while( $res=mysql_fetch_array($rsr))
> {
>
> echo "<tr><td>" . htmlspecialchars($ex[$j]) . "</td><td>" . htmlspecialchars($res[0]) . "</td> </td><td>" . htmlspecialchars($res[1]) . "</td></tr>" ;
>
> }
>
> $j +=21;
> $k +=21;
>
> }
>
>
> echo "</table></center>";
>
> mysql_close();
>
> ?>
>

OK, your code was posted in another part of the thread, so I wasn't sure
if that was what you were currently using or not.

There are several problems with your code:

1. Don't use '@' in front of function calls. It prevents you from
seeing any errors (even in the log). Rather, your development system
should have display_errors=on in your php.ini file, and your production
system should have display_errors=off.

2. The mysql extension has been deprecated. New code should use the
mysqli extension.

3. You shouldn't use die() in your web scripts. This terminates ALL
processing of your code, including any other html on the page. The
results is either a completely empty page (if your PHP code is at the
start of your page) or invalid HTML. Neither is good. Rather, if the
call fails, use a conditional to go around the code that relies on the
failing command.

4. You should ALWAYS have a root password on your database, even on your
local system.

5. The 'mysql' database is a system database and may be replaced or
changed by any upgrade. You should NEVER use it for your own data.
Create your own database instead.

6. You are depending on absolute positions for the start of your data.
This is almost never good.

7. You fetch the entire table, then loop through the entries but never
use them. Rather, you use this loop to execute additional SELECT
statements.

8. You make multiple calls to SELECT individual mac addresses. This is
less efficient than doing it all in one call.

9. You are not escaping the mac address before using it in the mysql
call. While it's not going to matter here (because the regex does
filtering), it's almost always good to use on strings for safety reasons.

Here is a better version. It uses a regex to get all of the ip and mac
addresses in an array, then selects them. I set this up for CLI output
(for basic testing here), so you'll have to change the display. But
otherwise it should work with few modifications and correcting for the
line wrapping as indicated

<?php

$arp = shell_exec('arp -a');

----

// *1
$res=preg_match_all('/^\s*([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})\ s+([0-9a-fA-F]{2}\-[0-9a-fA-F]{2}\-[0-9a-fA-F]{2}\-[0-9a-fA-F]{2}\-[0-9a-fA -F]{2}\-[0-9a-fA-F]{2})/m',
$arp, $matches, PREG_SET_ORDER);

// (the preceding should be all one line)

if ($res === false)
echo "Regex error!\n";
else { // *2
if ($link = mysqli_connect("localhost","root","password", "test")) {
// *3
$macs = ''; // *4
foreach ($matches as $match) {
if ($macs != '') // Not the first time through, so
$macs .= ', '; // Add a separator
$macs .= '\'' . mysqli_real_escape_string($link, $match[2]) .
'\''; // Append the next MAC address (again all one line above)
}
if ($macs != '') { // Ensure we actually had some *5
$sql = "SELECT mac_id, mac_name FROM mac_addr WHERE mac_id IN
($macs)"; // one line here also
$res = mysqli_query($link, $sql);
if ($res) { // .. If it worked
while ($data = mysqli_fetch_row($res))
echo $data['mac_id'] . ' ' . $data['mac_name'] . "\n";
}
else
echo "SELECT error: " . mysqli_error($link) . "\n";
}
}
else
echo "Connect error: " . mysqli_connect_error() . "\n";
}
?>

Notes:
1. This regex looks much worse than it is
2. $matches will be an array of arrays
Each array within $matches will contain:
[0] IP MAC_ADDR
[1] IP
[2] MAC_ADDR
3. Use mysqli instead of deprecated mysql.
You can also specify the database here (NOT mysql!)
And ensure you have a password
4. We could just issue multiple SELECT statements, one for each MAC
address. However, this is inefficient. Better is to use the IN
clause to
specify all MAC addresses in one SQL statement
5. $macs will now contain: 'mac1', 'mac2', 'mac3', ...' macx'


--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: Regarding split text and match from data base [message #183943 is a reply to message #183939] Thu, 28 November 2013 07:27 Go to previous messageGo to next message
Ben Bacarisse is currently offline  Ben Bacarisse
Messages: 82
Registered: November 2013
Karma: 0
Member
add to buddy list
ignore all messages by this user
Jerry Stuckle <jstucklex(at)attglobal(dot)net> writes:
<snip>
> Here is a better version. It uses a regex to get all of the ip and
> mac addresses in an array, then selects them. I set this up for CLI
> output (for basic testing here), so you'll have to change the display.
> But otherwise it should work with few modifications and correcting for
> the line wrapping as indicated
>
> <?php
>
> $arp = shell_exec('arp -a');
>
> ----
>
> // *1
> $res=preg_match_all('/^\s*([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})\ s+([0-9a-fA-F]{2}\-[0-9a-fA-F]{2}\-[0-9a-fA-F]{2}\-[0-9a-fA-F]{2}\-[0-9a-fA -F]{2}\-[0-9a-fA-F]{2})/m',
> $arp, $matches, PREG_SET_ORDER);

I'd do this a little differently. I'd use PREG_PATTERN_ORDER so that I
can just loop through $matches[2] without any further subscripting, but
I'd also not capture the IP address since it does not seem to be used.

A few other simplifications include using the i modifier rather than
writing a-fA-F, using \d rather than [0-9], not quoting '-' and using a
non-capturing group to specify the repeats:

$res = preg_match_all(
'/^\s*\d{1,3}(?:\.\d{1,3}){3}\s+([\dA-F]{2}(?:-[\dA-F]{2}){5})/mi',
$arp, $matches, PREG_PATTERN_ORDER);

As a result of changing the capture order, I'd also be tempted to
re-write the loop:

<snip>
> $macs = ''; // *4
> foreach ($matches as $match) {
> if ($macs != '') // Not the first time through, so
> $macs .= ', '; // Add a separator
> $macs .= '\'' . mysqli_real_escape_string($link, $match[2])
> . '\''; // Append the next MAC address (again all one line above)
> }

as $macs = "'" . join("', '", $matches[1]) . "'";

I don't claim any of these changes make the code better (except,
perhaps, the grouping in the regexp) but I think showing alternatives is
always a good thing.

<snip>
--
Ben.
Re: Regarding split text and match from data base [message #183945 is a reply to message #183943] Thu, 28 November 2013 11:01 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
add to buddy list
ignore all messages by this user
On 11/28/2013 7:27 AM, Ben Bacarisse wrote:
> Jerry Stuckle <jstucklex(at)attglobal(dot)net> writes:
> <snip>
>> Here is a better version. It uses a regex to get all of the ip and
>> mac addresses in an array, then selects them. I set this up for CLI
>> output (for basic testing here), so you'll have to change the display.
>> But otherwise it should work with few modifications and correcting for
>> the line wrapping as indicated
>>
>> <?php
>>
>> $arp = shell_exec('arp -a');
>>
>> ----
>>
>> // *1
>> $res=preg_match_all('/^\s*([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})\ s+([0-9a-fA-F]{2}\-[0-9a-fA-F]{2}\-[0-9a-fA-F]{2}\-[0-9a-fA-F]{2}\-[0-9a-fA -F]{2}\-[0-9a-fA-F]{2})/m',
>> $arp, $matches, PREG_SET_ORDER);
>
> I'd do this a little differently. I'd use PREG_PATTERN_ORDER so that I
> can just loop through $matches[2] without any further subscripting, but
> I'd also not capture the IP address since it does not seem to be used.
>

The OP indicated he wanted to capture the IP address. We don't know
what else he may be using it for. And I used PREG_SET_ORDER
specifically to keep the IP address with the MAC address.

> A few other simplifications include using the i modifier rather than
> writing a-fA-F, using \d rather than [0-9], not quoting '-' and using a
> non-capturing group to specify the repeats:
>

All good ideas. I was trying to keep it as simple as possible for the
OP's benefit.

> $res = preg_match_all(
> '/^\s*\d{1,3}(?:\.\d{1,3}){3}\s+([\dA-F]{2}(?:-[\dA-F]{2}){5})/mi',
> $arp, $matches, PREG_PATTERN_ORDER);
>
> As a result of changing the capture order, I'd also be tempted to
> re-write the loop:
>
> <snip>
>> $macs = ''; // *4
>> foreach ($matches as $match) {
>> if ($macs != '') // Not the first time through, so
>> $macs .= ', '; // Add a separator
>> $macs .= '\'' . mysqli_real_escape_string($link, $match[2])
>> . '\''; // Append the next MAC address (again all one line above)
>> }
>
> as $macs = "'" . join("', '", $matches[1]) . "'";
>

Which doesn't work when you capture both MAC and IP. Additionally, you
don't call mysqli_real_escape_string() for the values. Yes, I know it's
not actually required here - but it's a good habit for new programmers
to get into. It's much better to call it when not needed than to not
call it when it is needed.

> I don't claim any of these changes make the code better (except,
> perhaps, the grouping in the regexp) but I think showing alternatives is
> always a good thing.
>
> <snip>
>

No argument. All are good ideas if they fit into the OP's needs.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: Regarding split text and match from data base [message #183950 is a reply to message #183945] Thu, 28 November 2013 13:51 Go to previous messageGo to next message
Ben Bacarisse is currently offline  Ben Bacarisse
Messages: 82
Registered: November 2013
Karma: 0
Member
add to buddy list
ignore all messages by this user
Jerry Stuckle <jstucklex(at)attglobal(dot)net> writes:

> On 11/28/2013 7:27 AM, Ben Bacarisse wrote:
>> Jerry Stuckle <jstucklex(at)attglobal(dot)net> writes:
>> <snip>
>>> Here is a better version. It uses a regex to get all of the ip and
>>> mac addresses in an array, then selects them. I set this up for CLI
>>> output (for basic testing here), so you'll have to change the display.
>>> But otherwise it should work with few modifications and correcting for
>>> the line wrapping as indicated
>>>
>>> <?php
>>>
>>> $arp = shell_exec('arp -a');
>>>
>>> ----
>>>
>>> // *1
>>> $res=preg_match_all('/^\s*([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})\ s+([0-9a-fA-F]{2}\-[0-9a-fA-F]{2}\-[0-9a-fA-F]{2}\-[0-9a-fA-F]{2}\-[0-9a-fA -F]{2}\-[0-9a-fA-F]{2})/m',
>>> $arp, $matches, PREG_SET_ORDER);
>>
>> I'd do this a little differently. I'd use PREG_PATTERN_ORDER so that I
>> can just loop through $matches[2] without any further subscripting, but
>> I'd also not capture the IP address since it does not seem to be used.
>>
>
> The OP indicated he wanted to capture the IP address. We don't know
> what else he may be using it for. And I used PREG_SET_ORDER
> specifically to keep the IP address with the MAC address.

Without more code it's hard to tell, but for what I was commenting on,
pattern order looks better. The only advantage of either order is if
there is some natural operation that can be performed on $matches[i] and
there is no evidence for that as yet for 'set' order. There is a
natural operation that is done on all the macs, which it why, on the
balance of the current evidence, I'd favour pattern order.

<snip>
>> As a result of changing the capture order, I'd also be tempted to
>> re-write the loop:
>>
>> <snip>
>>> $macs = ''; // *4
>>> foreach ($matches as $match) {
>>> if ($macs != '') // Not the first time through, so
>>> $macs .= ', '; // Add a separator
>>> $macs .= '\'' . mysqli_real_escape_string($link, $match[2])
>>> . '\''; // Append the next MAC address (again all one line above)
>>> }
>>
>> as $macs = "'" . join("', '", $matches[1]) . "'";
>
> Which doesn't work when you capture both MAC and IP.

You can if you just write 2 instead of 1, but I suspect that's not what
you meant. You can't do this if you collect in 'set' order rather than
'pattern' order, but even if the IP address is captured there's no clear
reason to choose set order (so far as we know).

> Additionally,
> you don't call mysqli_real_escape_string() for the values. Yes, I
> know it's not actually required here - but it's a good habit for new
> programmers to get into. It's much better to call it when not needed
> than to not call it when it is needed.

Well, you can't mean that literally (there are places where it's not
needed where using it would break the code) but I know what you are
getting at. Part of the equation should be, in my view, how likely it
is that the quoting will be needed. Given the context (mac addresses)
it's not very likely, and I'd take that into account.

<snip>
--
Ben.
Re: Regarding split text and match from data base [message #183952 is a reply to message #183950] Thu, 28 November 2013 19:27 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
add to buddy list
ignore all messages by this user
On 11/28/2013 1:51 PM, Ben Bacarisse wrote:
> Jerry Stuckle <jstucklex(at)attglobal(dot)net> writes:
>
>> On 11/28/2013 7:27 AM, Ben Bacarisse wrote:
>>> Jerry Stuckle <jstucklex(at)attglobal(dot)net> writes:
>>> <snip>
>>>> Here is a better version. It uses a regex to get all of the ip and
>>>> mac addresses in an array, then selects them. I set this up for CLI
>>>> output (for basic testing here), so you'll have to change the display.
>>>> But otherwise it should work with few modifications and correcting for
>>>> the line wrapping as indicated
>>>>
>>>> <?php
>>>>
>>>> $arp = shell_exec('arp -a');
>>>>
>>>> ----
>>>>
>>>> // *1
>>>> $res=preg_match_all('/^\s*([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})\ s+([0-9a-fA-F]{2}\-[0-9a-fA-F]{2}\-[0-9a-fA-F]{2}\-[0-9a-fA-F]{2}\-[0-9a-fA -F]{2}\-[0-9a-fA-F]{2})/m',
>>>> $arp, $matches, PREG_SET_ORDER);
>>>
>>> I'd do this a little differently. I'd use PREG_PATTERN_ORDER so that I
>>> can just loop through $matches[2] without any further subscripting, but
>>> I'd also not capture the IP address since it does not seem to be used.
>>>
>>
>> The OP indicated he wanted to capture the IP address. We don't know
>> what else he may be using it for. And I used PREG_SET_ORDER
>> specifically to keep the IP address with the MAC address.
>
> Without more code it's hard to tell, but for what I was commenting on,
> pattern order looks better. The only advantage of either order is if
> there is some natural operation that can be performed on $matches[i] and
> there is no evidence for that as yet for 'set' order. There is a
> natural operation that is done on all the macs, which it why, on the
> balance of the current evidence, I'd favour pattern order.
>

Yes, it is hard to tell without more code. However, lacking that I
think set order is more appropriate, because the linkage between the ip
and the mac address is maintained.

> <snip>
>>> As a result of changing the capture order, I'd also be tempted to
>>> re-write the loop:
>>>
>>> <snip>
>>>> $macs = ''; // *4
>>>> foreach ($matches as $match) {
>>>> if ($macs != '') // Not the first time through, so
>>>> $macs .= ', '; // Add a separator
>>>> $macs .= '\'' . mysqli_real_escape_string($link, $match[2])
>>>> . '\''; // Append the next MAC address (again all one line above)
>>>> }
>>>
>>> as $macs = "'" . join("', '", $matches[1]) . "'";
>>
>> Which doesn't work when you capture both MAC and IP.
>
> You can if you just write 2 instead of 1, but I suspect that's not what
> you meant. You can't do this if you collect in 'set' order rather than
> 'pattern' order, but even if the IP address is captured there's no clear
> reason to choose set order (so far as we know).
>

No, because your regex did not parse out the ip. And again, pattern
order breaks the linkage between ip and mac address. I chose that
because if the OP wants the ip also, it would not make sense to use it
without the mac address.

>> Additionally,
>> you don't call mysqli_real_escape_string() for the values. Yes, I
>> know it's not actually required here - but it's a good habit for new
>> programmers to get into. It's much better to call it when not needed
>> than to not call it when it is needed.
>
> Well, you can't mean that literally (there are places where it's not
> needed where using it would break the code) but I know what you are
> getting at. Part of the equation should be, in my view, how likely it
> is that the quoting will be needed. Given the context (mac addresses)
> it's not very likely, and I'd take that into account.
>
> <snip>
>

Yes, I do mean that literally. And there is no place where you call it
for strings that will break the code. If there is, I would like you to
show it. mysqli_real_escape_string is about more than quoting.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: Regarding split text and match from data base [message #183955 is a reply to message #183952] Thu, 28 November 2013 21:34 Go to previous messageGo to next message
Ben Bacarisse is currently offline  Ben Bacarisse
Messages: 82
Registered: November 2013
Karma: 0
Member
add to buddy list
ignore all messages by this user
Jerry Stuckle <jstucklex(at)attglobal(dot)net> writes:

> On 11/28/2013 1:51 PM, Ben Bacarisse wrote:
>> Jerry Stuckle <jstucklex(at)attglobal(dot)net> writes:
>>
>>> On 11/28/2013 7:27 AM, Ben Bacarisse wrote:
<snip>
>>>> As a result of changing the capture order, I'd also be tempted to
>>>> re-write the loop:
>>>>
>>>> <snip>
>>>> > $macs = ''; // *4
>>>> > foreach ($matches as $match) {
>>>> > if ($macs != '') // Not the first time through, so
>>>> > $macs .= ', '; // Add a separator
>>>> > $macs .= '\'' . mysqli_real_escape_string($link, $match[2])
>>>> > . '\''; // Append the next MAC address (again all one line above)
>>>> > }
>>>>
>>>> as $macs = "'" . join("', '", $matches[1]) . "'";
>>>
>>> Which doesn't work when you capture both MAC and IP.
>>
>> You can if you just write 2 instead of 1, but I suspect that's not what
>> you meant. You can't do this if you collect in 'set' order rather than
>> 'pattern' order, but even if the IP address is captured there's no clear
>> reason to choose set order (so far as we know).
>
> No, because your regex did not parse out the ip.

It parses it, it just doesn't capture it. When you capture both MAC and
IP (to use your exact phrase) you just replace 1 with 2. Am I missing
your point?

> And again, pattern
> order breaks the linkage between ip and mac address. I chose that
> because if the OP wants the ip also, it would not make sense to use it
> without the mac address.

We should agree to disagree on this. I can think of ways in which the
IP address might be used where pattern order is still better (for
example, building another query but using IP addresses this time) but
there are also cases where set order will be more logical or helpful.
However, I made the suggestion based on what your code actually did.

Going back to the OP's code I see that he or she wants to print the IP
as well. Your loop was:

while ($data = mysqli_fetch_row($res))
echo $data['mac_id'] . ' ' . $data['mac_name'] . "\n";

and we can add the IP address just as easily with either capture
ordering.

>>> Additionally,
>>> you don't call mysqli_real_escape_string() for the values. Yes, I
>>> know it's not actually required here - but it's a good habit for new
>>> programmers to get into. It's much better to call it when not needed
>>> than to not call it when it is needed.
>>
>> Well, you can't mean that literally (there are places where it's not
>> needed where using it would break the code) but I know what you are
>> getting at. Part of the equation should be, in my view, how likely it
>> is that the quoting will be needed. Given the context (mac addresses)
>> it's not very likely, and I'd take that into account.
>>
>> <snip>
>
> Yes, I do mean that literally. And there is no place where you call
> it for strings that will break the code. If there is, I would like
> you to show it. mysqli_real_escape_string is about more than quoting.

I think we are talking at cross purposes. You mean (I think) that it's
never wrong to call it when it might be right to call it -- and that's
true. I took your "better to call it when not needed" absolutely
literally. For example, if the string is already quoted it is "not
needed" and calling it will break something.

--
Ben.
Re: Regarding split text and match from data base [message #183956 is a reply to message #183955] Thu, 28 November 2013 22:47 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
add to buddy list
ignore all messages by this user
On 11/28/2013 9:34 PM, Ben Bacarisse wrote:
> Jerry Stuckle <jstucklex(at)attglobal(dot)net> writes:
>
>> On 11/28/2013 1:51 PM, Ben Bacarisse wrote:
>>> Jerry Stuckle <jstucklex(at)attglobal(dot)net> writes:
>>>
>>>> On 11/28/2013 7:27 AM, Ben Bacarisse wrote:
> <snip>
>>>> > As a result of changing the capture order, I'd also be tempted to
>>>> > re-write the loop:
>>>> >
>>>> > <snip>
>>>> >> $macs = ''; // *4
>>>> >> foreach ($matches as $match) {
>>>> >> if ($macs != '') // Not the first time through, so
>>>> >> $macs .= ', '; // Add a separator
>>>> >> $macs .= '\'' . mysqli_real_escape_string($link, $match[2])
>>>> >> . '\''; // Append the next MAC address (again all one line above)
>>>> >> }
>>>> >
>>>> > as $macs = "'" . join("', '", $matches[1]) . "'";
>>>>
>>>> Which doesn't work when you capture both MAC and IP.
>>>
>>> You can if you just write 2 instead of 1, but I suspect that's not what
>>> you meant. You can't do this if you collect in 'set' order rather than
>>> 'pattern' order, but even if the IP address is captured there's no clear
>>> reason to choose set order (so far as we know).
>>
>> No, because your regex did not parse out the ip.
>
> It parses it, it just doesn't capture it. When you capture both MAC and
> IP (to use your exact phrase) you just replace 1 with 2. Am I missing
> your point?
>

Ah, OK, I understand you now. But it still doesn't work if you use set
order.

>> And again, pattern
>> order breaks the linkage between ip and mac address. I chose that
>> because if the OP wants the ip also, it would not make sense to use it
>> without the mac address.
>
> We should agree to disagree on this. I can think of ways in which the
> IP address might be used where pattern order is still better (for
> example, building another query but using IP addresses this time) but
> there are also cases where set order will be more logical or helpful.
> However, I made the suggestion based on what your code actually did.
>

My code was based on the OP's overall requirements. This is just a
small part of the whole script, I believe.

> Going back to the OP's code I see that he or she wants to print the IP
> as well. Your loop was:
>
> while ($data = mysqli_fetch_row($res))
> echo $data['mac_id'] . ' ' . $data['mac_name'] . "\n";
>
> and we can add the IP address just as easily with either capture
> ordering.
>

Very easy to do with set order. Each pair is matched up - ip address in
element 1 and mac address in element 2 of the array. It is very easy to
use foreach() to go through the base array and access element 1 and
element 2 of the internal array, as I did. If you use pattern order,
you can't do that.

But I think we are arguing a matter of style here. I also use pattern
order when appropriate. In this case I still think set order is more
appropriate.

>>>> Additionally,
>>>> you don't call mysqli_real_escape_string() for the values. Yes, I
>>>> know it's not actually required here - but it's a good habit for new
>>>> programmers to get into. It's much better to call it when not needed
>>>> than to not call it when it is needed.
>>>
>>> Well, you can't mean that literally (there are places where it's not
>>> needed where using it would break the code) but I know what you are
>>> getting at. Part of the equation should be, in my view, how likely it
>>> is that the quoting will be needed. Given the context (mac addresses)
>>> it's not very likely, and I'd take that into account.
>>>
>>> <snip>
>>
>> Yes, I do mean that literally. And there is no place where you call
>> it for strings that will break the code. If there is, I would like
>> you to show it. mysqli_real_escape_string is about more than quoting.
>
> I think we are talking at cross purposes. You mean (I think) that it's
> never wrong to call it when it might be right to call it -- and that's
> true. I took your "better to call it when not needed" absolutely
> literally. For example, if the string is already quoted it is "not
> needed" and calling it will break something.
>

No, if the string is already quoted, it will not "break something".
Only if the string has already been processed through
mysqli_real_escape_string() could there be a problem. Any other method
of "quoting" is not supported.

And yes, I will still recommend using mysqli_real_escape_string(), even
when not needed (at least right now it's not needed).

And yes, when working with people unfamiliar with a language (here or in
my classes), I ALWAYS go on the side of caution. It keeps people out of
trouble more often than not. They learn the conservative rules, and
later learn when it is OK to break those rules - rather than vice versa.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: Regarding split text and match from data base [message #183958 is a reply to message #183956] Fri, 29 November 2013 00:01 Go to previous messageGo to next message
Ben Bacarisse is currently offline  Ben Bacarisse
Messages: 82
Registered: November 2013
Karma: 0
Member
add to buddy list
ignore all messages by this user
Jerry Stuckle <jstucklex(at)attglobal(dot)net> writes:

> On 11/28/2013 9:34 PM, Ben Bacarisse wrote:
<snip>
>> Going back to the OP's code I see that he or she wants to print the IP
>> as well. Your loop was:
>>
>> while ($data = mysqli_fetch_row($res))
>> echo $data['mac_id'] . ' ' . $data['mac_name'] . "\n";
>>
>> and we can add the IP address just as easily with either capture
>> ordering.
>
> Very easy to do with set order. Each pair is matched up - ip address
> in element 1 and mac address in element 2 of the array.

The pairing is not needed -- that's why I quoted your own code. It is
just as easy to add the IP address to your output using either order.

> It is very
> easy to use foreach() to go through the base array and access element
> 1 and element 2 of the internal array, as I did. If you use pattern
> order, you can't do that.

Are we looking at different code? I don't see anywhere where you do
that.

You print the elements using a while statement that does not use the
matched values at all. Adding the IP address to the output can be done
as easily with either capture order. The only foreach loop I can see
was used to build list of MAC addresses and it has no need of the
corresponding IP address.

As it happens, I think that code is actually a little clearer using
pattern order. Rather than:

foreach ($matches as $match) {
if ($macs != '') // Not the first time through, so
$macs .= ', '; // Add a separator
$macs .= '\'' . mysqli_real_escape_string($link, $match[2])
. '\'';
}

you get:

foreach ($matches[2] as $match) {
if ($macs != '') // Not the first time through, so
$macs .= ', '; // Add a separator
$macs .= '\'' . X_mysqli_real_escape_string($link, $match)
. '\'';
}

where it's clear from the get-go that the loop is processing the MAC
addresses. There's no need to see what elements of $match get using in
the loop body to know what it's processing.

> But I think we are arguing a matter of style here. I also use pattern
> order when appropriate. In this case I still think set order is more
> appropriate.

As I said, we should agree to disagree. My conclusion is the reverse of
yours.

>>>> > Additionally,
>>>> > you don't call mysqli_real_escape_string() for the values. Yes, I
>>>> > know it's not actually required here - but it's a good habit for new
>>>> > programmers to get into. It's much better to call it when not needed
>>>> > than to not call it when it is needed.
>>>>
>>>> Well, you can't mean that literally (there are places where it's not
>>>> needed where using it would break the code) but I know what you are
>>>> getting at. Part of the equation should be, in my view, how likely it
>>>> is that the quoting will be needed. Given the context (mac addresses)
>>>> it's not very likely, and I'd take that into account.
>>>>
>>>> <snip>
>>>
>>> Yes, I do mean that literally. And there is no place where you call
>>> it for strings that will break the code. If there is, I would like
>>> you to show it. mysqli_real_escape_string is about more than quoting.
>>
>> I think we are talking at cross purposes. You mean (I think) that it's
>> never wrong to call it when it might be right to call it -- and that's
>> true. I took your "better to call it when not needed" absolutely
>> literally. For example, if the string is already quoted it is "not
>> needed" and calling it will break something.
>
> No, if the string is already quoted, it will not "break
> something". Only if the string has already been processed through
> mysqli_real_escape_string() could there be a problem. Any other
> method of "quoting" is not supported.

I just don't get this. If I have some data that is already quoted it
must be inserted it into an SQL statement as is. If I pass it through
mysqli_real_escape_string it will not longer be correctly quoted.

But, anyway, you've just given an example yourself of a situation where
it is not better to cal is when not needed -- when the data has, at some
point, already been through mysqli_real_escape_string.

<snip>
--
Ben.
Re: Regarding split text and match from data base [message #183963 is a reply to message #183958] Fri, 29 November 2013 07:40 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
add to buddy list
ignore all messages by this user
On 11/29/2013 12:01 AM, Ben Bacarisse wrote:
> Jerry Stuckle <jstucklex(at)attglobal(dot)net> writes:
>
>> On 11/28/2013 9:34 PM, Ben Bacarisse wrote:
> <snip>
>>> Going back to the OP's code I see that he or she wants to print the IP
>>> as well. Your loop was:
>>>
>>> while ($data = mysqli_fetch_row($res))
>>> echo $data['mac_id'] . ' ' . $data['mac_name'] . "\n";
>>>
>>> and we can add the IP address just as easily with either capture
>>> ordering.
>>
>> Very easy to do with set order. Each pair is matched up - ip address
>> in element 1 and mac address in element 2 of the array.
>
> The pairing is not needed -- that's why I quoted your own code. It is
> just as easy to add the IP address to your output using either order.
>

You can't easily do it with foreach(), for instance. i.e.

foreach($matches as $match) {
$ip = mysqli_real_escape_string($link, $match[1]);
$mac = mysqli_real_escape_string($link, $match[2]);
mysqli_query($link, 'INSERT INTO myTable(ip,mac) ' .
"VALUES ('$ip', '$mac')";

>> It is very
>> easy to use foreach() to go through the base array and access element
>> 1 and element 2 of the internal array, as I did. If you use pattern
>> order, you can't do that.
>
> Are we looking at different code? I don't see anywhere where you do
> that.
>

No, I'm not doing it here. But that doesn't mean it isn't necessary.
He wanted the IP address for a reason.

> You print the elements using a while statement that does not use the
> matched values at all. Adding the IP address to the output can be done
> as easily with either capture order. The only foreach loop I can see
> was used to build list of MAC addresses and it has no need of the
> corresponding IP address.
>

Not in this case. But that doesn't mean it isn't necessary. He wanted
the IP address for a reason.

> As it happens, I think that code is actually a little clearer using
> pattern order. Rather than:
>
> foreach ($matches as $match) {
> if ($macs != '') // Not the first time through, so
> $macs .= ', '; // Add a separator
> $macs .= '\'' . mysqli_real_escape_string($link, $match[2])
> . '\'';
> }
>
> you get:
>
> foreach ($matches[2] as $match) {
> if ($macs != '') // Not the first time through, so
> $macs .= ', '; // Add a separator
> $macs .= '\'' . X_mysqli_real_escape_string($link, $match)
> . '\'';
> }
>

AI really don't see where this is clearer. So you're not using
$matches[2] instead of in $match[2]. It is immaterial.

> where it's clear from the get-go that the loop is processing the MAC
> addresses. There's no need to see what elements of $match get using in
> the loop body to know what it's processing.
>

It's also clear from my loop that you are processing the mac addresses.
In your case you still need to know it's in $matches[2].

>> But I think we are arguing a matter of style here. I also use pattern
>> order when appropriate. In this case I still think set order is more
>> appropriate.
>
> As I said, we should agree to disagree. My conclusion is the reverse of
> yours.
>

Yes, and you won't give it up, will you?

>>>> >> Additionally,
>>>> >> you don't call mysqli_real_escape_string() for the values. Yes, I
>>>> >> know it's not actually required here - but it's a good habit for new
>>>> >> programmers to get into. It's much better to call it when not needed
>>>> >> than to not call it when it is needed.
>>>> >
>>>> > Well, you can't mean that literally (there are places where it's not
>>>> > needed where using it would break the code) but I know what you are
>>>> > getting at. Part of the equation should be, in my view, how likely it
>>>> > is that the quoting will be needed. Given the context (mac addresses)
>>>> > it's not very likely, and I'd take that into account.
>>>> >
>>>> > <snip>
>>>>
>>>> Yes, I do mean that literally. And there is no place where you call
>>>> it for strings that will break the code. If there is, I would like
>>>> you to show it. mysqli_real_escape_string is about more than quoting.
>>>
>>> I think we are talking at cross purposes. You mean (I think) that it's
>>> never wrong to call it when it might be right to call it -- and that's
>>> true. I took your "better to call it when not needed" absolutely
>>> literally. For example, if the string is already quoted it is "not
>>> needed" and calling it will break something.
>>
>> No, if the string is already quoted, it will not "break
>> something". Only if the string has already been processed through
>> mysqli_real_escape_string() could there be a problem. Any other
>> method of "quoting" is not supported.
>
> I just don't get this. If I have some data that is already quoted it
> must be inserted it into an SQL statement as is. If I pass it through
> mysqli_real_escape_string it will not longer be correctly quoted.
>

The only valid way to quote a string is with mysqli_real_escape_string()
(or the deprecated mysql_escape_string). Of course you would not expect
to get the correct results if you process a string two times though the
function.

> But, anyway, you've just given an example yourself of a situation where
> it is not better to cal is when not needed -- when the data has, at some
> point, already been through mysqli_real_escape_string.
>
> <snip>
>

Then you should already know that. That's only a matter of common
sense. But you are still processing it with mysqli_real_escape_string().

But it seems you feel the need to argue just to argue.


--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: Regarding split text and match from data base [message #183965 is a reply to message #183963] Fri, 29 November 2013 08:58 Go to previous message
Ben Bacarisse is currently offline  Ben Bacarisse
Messages: 82
Registered: November 2013
Karma: 0
Member
add to buddy list
ignore all messages by this user
Jerry Stuckle <jstucklex(at)attglobal(dot)net> writes:

> On 11/29/2013 12:01 AM, Ben Bacarisse wrote:
>> Jerry Stuckle <jstucklex(at)attglobal(dot)net> writes:
<snip>
>>> It is very
>>> easy to use foreach() to go through the base array and access element
>>> 1 and element 2 of the internal array, as I did. If you use pattern
>>> order, you can't do that.
>>
>> Are we looking at different code? I don't see anywhere where you do
>> that.
>
> No, I'm not doing it here. But that doesn't mean it isn't
> necessary.

To what code were you referring to when you said "as i did"?

> He wanted the IP address for a reason.

Yes, and the original post showed what for. Of course he might really
want to do something else entirely, but it's not possible get anywhere
staring with that assumption.

<snip>
>> As I said, we should agree to disagree. My conclusion is the reverse of
>> yours.
>
> Yes, and you won't give it up, will you?

I suggested that we agree to disagree two posts ago, but you did not
want to -- you explained, again, why your choice was better. I won't
advocate for my point of view (you'll notice that I don't do so in this
post) if you won't.

<snip>
--
Ben.
Quick Reply
Formatting Tools:   
  Switch to threaded view of this topic Create a new topic
Previous Topic: creating key / hash
Next Topic: from mysql in associative array
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Thu Dec 14 16:07:16 EST 2017

Total time taken to generate the page: 0.01190 seconds