尖括号 mysql php [英] angle bracket mysql php

查看:42
本文介绍了尖括号 mysql php的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当 VARCHAR 字段内的文本被尖括号('<' 和 '>' 字符)包围时,我无法从 MySQL 数据库中获取数据.

让我说得更具体一点.这是我从中获取数据的表:

<前>mysql> 描述信息;+--------------+--------------------+------+-----+---------+-------+|领域 |类型 |空 |钥匙 |默认 |额外 |+--------------+--------------------+------+-----+---------+-------+|partition_tag |整数(11) |否 |PRI |0 |||mail_id |varbinary(16) |否 |PRI |空 |||secret_id |varbinary(16) |是 |||||am_id |varchar(20) |否 ||空 |||time_num |int(10) 无符号 |否 |多|空 |||time_iso |字符(16) |否 ||空 |||西德 |bigint(20) 无符号 |否 |多|空 |||政策 |varchar(255) |是 |||||客户地址 |varchar(255) |是 |||||尺寸 |int(10) 无符号 |否 ||空 |||起源 |字符(1) |否 |||||内容 |字符(1) |是 ||空 |||quar_type |字符(1) |是 ||空 |||quar_loc |varbinary(255) |是 |||||dsn_sent |字符(1) |是 ||空 |||spam_level |浮动|是 ||空 |||message_id |varchar(255) |是 |多||||from_addr |varchar(255) |是 ||空 |||主题 |varchar(255) |是 |||||主机|varchar(255) |否 ||空 ||+--------------+--------------------+------+-----+---------+-------+mysql> select * from msgs LIMIT 1;+--------------+--------------+--------------+----------+------------+------------+-----+--------+--------------+------+------------+---------+-----------+--------------+---------+------------+------------------------------------------+----------------------------------------+---------+-------------------+|partition_tag |mail_id |secret_id |am_id |time_num |time_iso |西德 |政策 |客户地址 |尺寸 |起源 |内容 |quar_type |quar_loc |dsn_sent |spam_level |message_id |from_addr |主题 |主机|+--------------+--------------+--------------+----------+------------+------------+-----+--------+--------------+------+------------+---------+-----------+--------------+---------+------------+------------------------------------------+----------------------------------------+---------+-------------------+|0 |0qJcjCXZM8K3 |7a8Q1_crCzuj |02085-01 |1365578237 |20130410T071717Z |4 |MYNETS |172.31.255.5 |第1246章是 |V |问 |0qJcjCXZM8K3 |否 |-1 |<51651189.9080705@test.it>|用户名 |测试 |本地主机 |+--------------+--------------+--------------+----------+------------+------------+-----+--------+--------------+------+------------+---------+-----------+--------------+---------+------------+------------------------------------------+----------------------------------------+---------+-------------------+

当通过 PHP MySQLi 接口(例如:SELECT * from msgs)执行查询时,from_addr 字段只显示第一部分(在本例中为用户名"),但显示里面的文本尖括号(在本例中为test@test.it").

目前,我通过在 mysql 查询中使用 REPLACE 语句来解决它,用方括号替换尖括号.但是,我想知道如何正确查询尖括号内的文本.

谢谢大家.

解决方案

如果您收到查询结果,from_addr 的输出将在那里.

看看这个例子:

$string = '';回声 $string;echo htmlspecialchars('<hello world>');

然而,echo $string 将显示在源代码中,但被视为一个标签,因此不会在屏幕上"显示.

使用 htmlspecialchars() 将改变 <>&lt;&gt; 将显示在屏幕上".

您似乎将此字段用于电子邮件,因此我不建议使用 htmlspecialchars 保存到数据库,因为您希望电子邮件正确发送.

如果以上没有完全回答您的问题,并且您希望查询数据库中的电子邮件而不是发送名称",我建议使用 LIKE:

SELECT * FROM `msgs` WHERE `from_addr` LIKE '<%INSERT_EMAIL_ADDRESS%>'.

希望这会有所帮助.

I have troubles fetching data from a MySQL database when the text inside a VARCHAR field is surrounded by angle brackets ('<' and '>' characters).

Let me be more specific. This is the table from which I'm fetching data:


    mysql> describe msgs;
    +---------------+---------------------+------+-----+---------+-------+
    | Field         | Type                | Null | Key | Default | Extra |
    +---------------+---------------------+------+-----+---------+-------+
    | partition_tag | int(11)             | NO   | PRI | 0       |       |
    | mail_id       | varbinary(16)       | NO   | PRI | NULL    |       |
    | secret_id     | varbinary(16)       | YES  |     |         |       |
    | am_id         | varchar(20)         | NO   |     | NULL    |       |
    | time_num      | int(10) unsigned    | NO   | MUL | NULL    |       |
    | time_iso      | char(16)            | NO   |     | NULL    |       |
    | sid           | bigint(20) unsigned | NO   | MUL | NULL    |       |
    | policy        | varchar(255)        | YES  |     |         |       |
    | client_addr   | varchar(255)        | YES  |     |         |       |
    | size          | int(10) unsigned    | NO   |     | NULL    |       |
    | originating   | char(1)             | NO   |     |         |       |
    | content       | char(1)             | YES  |     | NULL    |       |
    | quar_type     | char(1)             | YES  |     | NULL    |       |
    | quar_loc      | varbinary(255)      | YES  |     |         |       |
    | dsn_sent      | char(1)             | YES  |     | NULL    |       |
    | spam_level    | float               | YES  |     | NULL    |       |
    | message_id    | varchar(255)        | YES  | MUL |         |       |
    | from_addr     | varchar(255)        | YES  |     | NULL    |       |
    | subject       | varchar(255)        | YES  |     |         |       |
    | host          | varchar(255)        | NO   |     | NULL    |       |
    +---------------+---------------------+------+-----+---------+-------+

    mysql> select * from msgs LIMIT 1;
    +---------------+--------------+--------------+----------+------------+------------------+-----+--------+--------------+------+-------------+---------+-----------+--------------+----------+------------+-------------------------------+----------------------------------------+---------+--------------------+
    | partition_tag | mail_id      | secret_id    | am_id    | time_num   | time_iso         | sid | policy | client_addr  | size | originating | content | quar_type | quar_loc     | dsn_sent | spam_level | message_id                    | from_addr                              | subject | host               |
    +---------------+--------------+--------------+----------+------------+------------------+-----+--------+--------------+------+-------------+---------+-----------+--------------+----------+------------+-------------------------------+----------------------------------------+---------+--------------------+
    |             0 | 0qJcjCXZM8K3 | 7a8Q1_crCzuj | 02085-01 | 1365578237 | 20130410T071717Z |   4 | MYNETS | 172.31.255.5 | 1246 | Y           | V       | Q         | 0qJcjCXZM8K3 | N        |         -1 | <51651189.9080705@test.it>    | User Name <test@test.it>               |  test   |  localhost         |
    +---------------+--------------+--------------+----------+------------+------------------+-----+--------+--------------+------+-------------+---------+-----------+--------------+----------+------------+-------------------------------+----------------------------------------+---------+--------------------+

When executing a query via PHP MySQLi interface (eg: SELECT * from msgs), the from_addr field show only the first part (in this case "User Name") but not the text inside the angle brackets ("test@test.it", in this case).

At the moment, I worked around it by using a REPLACE statement inside mysql query, substituting the angle brackets with square brackets. However, I would like to know how to correctly query text inside angle brackets.

Thank you all.

解决方案

The output of from_addr will be there if you are receiving results from your query.

Take a look at this example:

$string = '<hello world>';
echo $string;
echo htmlspecialchars('<hello world>');

Whereas, echo $string will show in the source code, but is being treated as a tag, and therefore not display "on screen".

Using the htmlspecialchars() will change < > to &lt; &gt; which will show "on screen".

It appears that you are using this field for email, so I would NOT recommend saving to the database with htmlspecialchars, as you want the emails to send properly.

If the above didn't answer your question fully, and you wish to query the database for an email rather than the "send name", I would recommend using LIKE:

SELECT * FROM `msgs` WHERE `from_addr` LIKE '<%INSERT_EMAIL_ADDRESS%>'.

Hope this helps.

这篇关于尖括号 mysql php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆