Varchar数据有选择地显示结果 [英] Varchar data shows results selectively
问题描述
ip mac
1 2
1.10.0.0.43 00 19 78 D3 R5 ED
8.9 32.22
中间一行是我期望来自开关的条目的样子.我将第一个和第三个条目用于测试目的.
我已经编写了一个php代码来进行匹配和检索,如下所示
<?php
$var = $_REQUEST['IP'];
echo $var;
mysql_connect( "localhost", "root", "mysql" ) or die("Unable to connect to the database");
mysql_select_db( "syslog" );
$result = mysql_query( "SELECT ip,mac FROM arp_table where ip=$var" );
$row = mysql_fetch_row( $result );
print_r( $row );
?>
当我在表格上输入1时,结果正常,以1 Array([0] => 1 [1] => 2)的形式,当我输入8.9时,输出也正常.但是,当我输入1.10.0.0.43时,输出仅是变量的回显,而不会检索到任何数组.有人知道为什么会这样吗?
这是因为您没有在$var
周围使用引号.在SQL中使用字符串时,需要用引号将它们引起来,例如:
SELECT ip,mac FROM arp_table where ip='$var';
如果不使用引号,则您的值将由MySQL解释并会自动转换为(例如)integer
,因为该值看起来像整数1
/2
...等. /p>
i am developing an application to monitor ip adress activity. I am using a LAMP stack on Ubuntu 12.04. In the mysql database, i created a table with 2 columns , ip and mac, both have datatype varchar. i put some data into the table and when i use select * from table, this is the result
ip mac
1 2
1.10.0.0.43 00 19 78 D3 R5 ED
8.9 32.22
The middle row is how i expect the entries that will come from a switch to look like.The first and 3rd i put them for testing purposes.
I have written a php code to do the matching and retrieval and it is as follows
<?php
$var = $_REQUEST['IP'];
echo $var;
mysql_connect( "localhost", "root", "mysql" ) or die("Unable to connect to the database");
mysql_select_db( "syslog" );
$result = mysql_query( "SELECT ip,mac FROM arp_table where ip=$var" );
$row = mysql_fetch_row( $result );
print_r( $row );
?>
when i enter 1 on the form ,the result is ok, in the form of 1 Array([0]=>1[1]=>2) and when i enter 8.9, the output is ok as well. But when i enter 1.10.0.0.43, the output is only the echo of the variable, no array is retrieved. Anyone with an idea of why this is so?
That is because you are not using quotes around $var
. When using strings in SQL they need to be quoted such as:
SELECT ip,mac FROM arp_table where ip='$var';
If they are not quoted then your value will be interpreted by MySQL and will be automatically converted to (for example) integer
because the value looks like an integer 1
/ 2
... etc..
这篇关于Varchar数据有选择地显示结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!