最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

mysql query - how to escape apostrophe?

programmeradmin0浏览0评论

I've got this query:

$tenantsInfo = $wpdb->get_results("SELECT * FROM exp_ten WHERE tenant_number = " . (int) $user->ID);

I use some echo to show data on frontend:

if ($tenantsInfo) {
        foreach ($tenantsInfo as $tenant) { 
           echo "<h2>Welcome," . " " .$tenant->tenant_name. "</h2>";

When data, like tenant's name contains apostrophe, no data is displayed on frontend.

Could somebody please help me change mysql statement so apostrophe could be escaped? Or maybe there is different solution?

Thanks in advance!

I've got this query:

$tenantsInfo = $wpdb->get_results("SELECT * FROM exp_ten WHERE tenant_number = " . (int) $user->ID);

I use some echo to show data on frontend:

if ($tenantsInfo) {
        foreach ($tenantsInfo as $tenant) { 
           echo "<h2>Welcome," . " " .$tenant->tenant_name. "</h2>";

When data, like tenant's name contains apostrophe, no data is displayed on frontend.

Could somebody please help me change mysql statement so apostrophe could be escaped? Or maybe there is different solution?

Thanks in advance!

Share Improve this question asked Nov 6, 2017 at 16:24 webhappywebhappy 113 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 1

Use esc_html() when outputting a string value inside tags. Use esc_attr() when you're outputting a string value inside an attribute="". If you're outputting a URL, use esc_url() instead of those two.

if ($tenantsInfo) {
        foreach ($tenantsInfo as $tenant) { 
           echo "<h2>Welcome, " . esc_html( $tenant->tenant_name ) . "</h2>";
        }
}

As this is not clear how you get the data inside the exp_ten table I would suggest use the esc_attr( $tenant->tenant_name ) which will encode the apostrophe along other things.

More info

发布评论

评论列表(0)

  1. 暂无评论