{"id":230291,"date":"2022-11-29T12:52:00","date_gmt":"2022-11-29T09:52:00","guid":{"rendered":"https:\/\/wordpress.mediadoma.com\/?p=230291"},"modified":"2022-11-09T20:58:15","modified_gmt":"2022-11-09T17:58:15","slug":"vikoristannja-sprintf-i-like-u-zapitah-sql-u-wordpress","status":"publish","type":"post","link":"https:\/\/wordpress.mediadoma.com\/uk\/vikoristannja-sprintf-i-like-u-zapitah-sql-u-wordpress\/","title":{"rendered":"\u0412\u0438\u043a\u043e\u0440\u0438\u0441\u0442\u0430\u043d\u043d\u044f sprintf \u0456 LIKE \u0443 \u0437\u0430\u043f\u0438\u0442\u0430\u0445 SQL \u0443 WordPress"},"content":{"rendered":"\n<p>\u041f\u0440\u0430\u0446\u044e\u044e\u0447\u0438 \u0456\u0437 \u0437\u0430\u043f\u0438\u0442\u0430\u043c\u0438 \u0432 WordPress, \u0437\u0430\u0432\u0436\u0434\u0438 \u043a\u0440\u0430\u0449\u0435 <a href=\"https:\/\/developer.wordpress.org\/reference\/classes\/wpdb\/prepare\/\" target=\"_blank\" rel=\"noopener nofollow\" class=\"external external_icon\">\u043f\u0456\u0434\u0433\u043e\u0442\u0443\u0432\u0430\u0442\u0438<\/a> \u0437\u0430\u043f\u0438\u0442 \u043f\u0435\u0440\u0435\u0434 \u0439\u043e\u0433\u043e \u0432\u0438\u043a\u043e\u043d\u0430\u043d\u043d\u044f\u043c; \u043e\u0434\u043d\u0430\u043a, \u044f\u043a\u0449\u043e \u0432\u0438 \u043c\u0430\u0454\u0442\u0435 \u0432\u0437\u0430\u0454\u043c\u043e\u0434\u0456\u044e \u0437\u0456 \u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0456\u043c API \u0430\u0431\u043e \u043f\u0440\u0430\u0446\u044e\u0454\u0442\u0435 \u0437 \u0456\u043d\u0448\u0438\u043c \u0432\u0430\u0440\u0456\u0430\u043d\u0442\u043e\u043c SQL (\u043d\u0430\u043f\u0440\u0438\u043a\u043b\u0430\u0434, Transact-SQL), \u0442\u043e\u0434\u0456 \u0432\u0441\u0435 \u043c\u043e\u0436\u0435 \u043f\u0440\u0430\u0446\u044e\u0432\u0430\u0442\u0438 \u0434\u0435\u0449\u043e \u0456\u043d\u0430\u043a\u0448\u0435.<\/p>\n<p>\u042f\u043a\u0431\u0438 \u0442\u0456\u043b\u044c\u043a\u0438 \u0431\u0430\u0437\u0438 \u0434\u0430\u043d\u0438\u0445 \u0431\u0443\u043b\u0438 \u0441\u043f\u0440\u0430\u0432\u0434\u0456 \u0442\u0430\u043a\u0438\u043c\u0438 \u0433\u0430\u0440\u043d\u0438\u043c\u0438.<\/p>\n<p><strong>TL;DR<\/strong> \u0432\u0441\u044c\u043e\u0433\u043e \u0446\u044c\u043e\u0433\u043e \u043f\u043e\u043b\u044f\u0433\u0430\u0454 \u0432 \u0442\u043e\u043c\u0443, \u0449\u043e \u043e\u0431\u0440\u043e\u0431\u043a\u0430 sprintf <a href=\"https:\/\/php.net\/manual\/en\/function.sprintf.php\" target=\"_blank\" rel=\"noopener nofollow\" class=\"external external_icon\">\u0456<\/a> LIKE <strong><a href=\"https:\/\/docs.microsoft.com\/en-us\/sql\/t-sql\/language-elements\/like-transact-sql?view=sql-server-2017\" target=\"_blank\" rel=\"noopener nofollow\" class=\"external external_icon\">\u0443 T-SQL<\/a><\/strong> \u043f\u043e\u0442\u0440\u0435\u0431\u0443\u0454 \u043f\u0435\u0432\u043d\u043e\u0457 \u0441\u0442\u0440\u0430\u0442\u0435\u0433\u0456\u0447\u043d\u043e\u0457 \u043e\u0431\u0440\u043e\u0431\u043a\u0438. \u0410 \u0437\u0440\u0430\u0437\u043e\u043a \u043a\u043e\u0434\u0443 \u0432 \u043d\u0430\u0441\u0442\u0443\u043f\u043d\u043e\u043c\u0443 \u0440\u043e\u0437\u0434\u0456\u043b\u0456 \u043c\u0430\u0454 \u043d\u0430\u0434\u0430\u0442\u0438 \u0432\u0441\u0435, \u0449\u043e \u0432\u0430\u043c \u043f\u043e\u0442\u0440\u0456\u0431\u043d\u043e.<\/p>\n<h2>sprintf \u0456 LIKE \u0432 SQL<\/h2>\n<p>\u0422\u0440\u043e\u0445\u0438 \u043f\u0435\u0440\u0435\u0434\u0456\u0441\u0442\u043e\u0440\u0456\u0457 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0438, \u0449\u043e \u0440\u043e\u0437\u0433\u043b\u044f\u0434\u0430\u0454\u0442\u044c\u0441\u044f:<\/p>\n<p>\u0421\u043a\u0430\u0436\u0456\u043c\u043e, \u0432\u0438 \u043f\u0440\u0430\u0446\u044e\u0454\u0442\u0435 \u043d\u0430\u0434 \u043f\u0440\u043e\u0435\u043a\u0442\u043e\u043c, \u0443 \u044f\u043a\u043e\u043c\u0443 \u0432\u0438 \u0437\u0432\u0435\u0440\u0442\u0430\u0454\u0442\u0435\u0441\u044c \u0434\u043e \u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044c\u043e\u0433\u043e API, \u0456 \u0446\u0435\u0439 API \u0432\u0438\u043a\u043e\u0440\u0438\u0441\u0442\u043e\u0432\u0443\u0454 T-SQL \u0434\u043b\u044f \u0432\u0437\u0430\u0454\u043c\u043e\u0434\u0456\u0457 \u0437\u0456 \u0441\u0432\u043e\u0454\u044e \u0431\u0430\u0437\u043e\u044e \u0434\u0430\u043d\u0438\u0445.<\/p>\n<p>\u0414\u0430\u043b\u0456, \u043f\u0440\u0438\u043f\u0443\u0441\u0442\u0456\u043c\u043e, \u0449\u043e \u0432\u0438 \u0445\u043e\u0447\u0435\u0442\u0435 \u043e\u0442\u0440\u0438\u043c\u0430\u0442\u0438 \u0437\u0430\u043f\u0438\u0441\u0438, \u044f\u043a\u0456 \u043c\u0456\u0441\u0442\u044f\u0442\u044c \u0441\u0435\u0433\u043c\u0435\u043d\u0442 \u0440\u044f\u0434\u043a\u0430, \u044f\u043a\u0438\u0439 \u043f\u043e\u0442\u0440\u0456\u0431\u043d\u043e \u0437\u0430\u043f\u0438\u0442\u0430\u0442\u0438 (\u0432\u0438 \u0437\u043d\u0430\u0454\u0442\u0435, \u043f\u0440\u043e\u043f\u043e\u0437\u0438\u0446\u0456\u044f <strong>LIKE<\/strong> ). \u0417\u0430\u0441\u0442\u0435\u0440\u0435\u0436\u0435\u043d\u043d\u044f \u043f\u043e\u043b\u044f\u0433\u0430\u0454 \u0432 \u0442\u043e\u043c\u0443, \u0449\u043e \u0432\u0438 \u043d\u0435 \u043c\u043e\u0436\u0435\u0442\u0435 \u0432\u0438\u043a\u043e\u0440\u0438\u0441\u0442\u043e\u0432\u0443\u0432\u0430\u0442\u0438 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u0443 \u0444\u0443\u043d\u043a\u0446\u0456\u044e <strong>\u043f\u0456\u0434\u0433\u043e\u0442\u043e\u0432\u043a\u0438, \u044f\u043a\u0443 \u043d\u0430\u0434\u0430\u0454 WordPress, \u0442\u043e\u043c\u0443 \u0432\u0438 \u0432\u0438\u0440\u0456\u0448\u0443\u0454\u0442\u0435 \u0432\u0438\u043a\u043e\u0440\u0438\u0441\u0442\u043e\u0432\u0443\u0432\u0430\u0442\u0438<\/strong> <strong>sprintf<\/strong>, \u0449\u043e\u0431 \u0434\u043e\u043f\u043e\u043c\u043e\u0433\u0442\u0438 \u043e\u0447\u0438\u0441\u0442\u0438\u0442\u0438 \u0432\u0445\u0456\u0434\u043d\u0443 \u0456\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0456\u044e.<\/p>\n<p>\u041e\u0441\u043a\u0456\u043b\u044c\u043a\u0438 <strong>sprintf<\/strong> \u0432\u0438\u043a\u043e\u0440\u0438\u0441\u0442\u043e\u0432\u0443\u0454 <code>%s<\/code>\u0434\u043b\u044f \u0440\u044f\u0434\u043a\u0456\u0432 \u0456 \u043e\u0441\u043a\u0456\u043b\u044c\u043a\u0438 \u0432\u0438 \u0432\u0438\u043a\u043e\u0440\u0438\u0441\u0442\u043e\u0432\u0443\u0454\u0442\u0435 \u043f\u0440\u043e\u043f\u043e\u0437\u0438\u0446\u0456\u044e <strong>LIKE<\/strong>, \u044f\u043a\u0430 \u0442\u0430\u043a\u043e\u0436 \u0432\u0438\u043c\u0430\u0433\u0430\u0454 <code>%<\/code>, \u044f\u043a \u043c\u043e\u0436\u043d\u0430 \u0432\u043f\u043e\u0440\u0430\u0442\u0438\u0441\u044f \u0437 \u0432\u0438\u043a\u043e\u0440\u0438\u0441\u0442\u0430\u043d\u043d\u044f\u043c \u043e\u0431\u043e\u0445?<\/p>\n<p><strong><a href=\"https:\/\/gist.github.com\/tommcfarlin\/b4d5a391024b151667fe99f5bd913544#file-00-get-user-by-last-name-php\" target=\"_blank\" rel=\"noopener nofollow\" class=\"external external_icon\">\u041e\u0441\u044c \u043f\u0440\u0438\u043a\u043b\u0430\u0434<\/a><\/strong> \u0442\u043e\u0433\u043e, \u044f\u043a \u0446\u0435 \u043c\u043e\u0436\u0435 \u043f\u0440\u0430\u0446\u044e\u0432\u0430\u0442\u0438 \u0434\u043b\u044f \u0432\u0430\u0441:<\/p>\n<pre><code>&lt;?php\n\/**\n * Queries a third-party API using sprintf and a LIKE clause to retrieve the users \n * with a last name like the specified argument.\n * \n * @param string $lastName An unsanitized version of the user's last name.\n *\/\npublic function getUserByLastName(string $lastName)\n{\n    \/\/ Sanitize the argument as you see fit here.\n\n    $query = sprintf(\"\n        SELECT FIRST_NAME, LAST_NAME\n        FROM USERINFO\n        WHERE LAST_NAME LIKE '%%%s%%'\n    \", $lastName);\n\n    \/\/ Make a query to the third-party API here.\n}\n<\/code><\/pre>\n<p>\u0417\u0432\u0438\u0447\u0430\u0439\u043d\u043e, \u044f \u0432\u0441\u0435 \u0449\u0435 \u0434\u0443\u043c\u0430\u044e \u043f\u0440\u043e \u0432\u0438\u043a\u043e\u0440\u0438\u0441\u0442\u0430\u043d\u043d\u044f \u043f\u0435\u0432\u043d\u043e\u0433\u043e \u0442\u0438\u043f\u0443 \u0435\u043a\u0440\u0430\u043d\u0443\u0432\u0430\u043d\u043d\u044f <code>$name<\/code>\u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u0443, \u0430\u043b\u0435 \u0454 \u043a\u0456\u043b\u044c\u043a\u0430 \u0440\u0456\u0437\u043d\u0438\u0445 \u0441\u043f\u043e\u0441\u043e\u0431\u0456\u0432 \u0437\u0440\u043e\u0431\u0438\u0442\u0438 \u0446\u0435, \u0456 \u0446\u0435 \u0432\u0438\u0445\u043e\u0434\u0438\u0442\u044c \u0437\u0430 \u043c\u0435\u0436\u0456 \u0446\u0456\u0454\u0457 \u043f\u0443\u0431\u043b\u0456\u043a\u0430\u0446\u0456\u0457.<\/p>\n<p>\u041a\u043e\u0440\u043e\u0442\u0448\u0435 \u043a\u0430\u0436\u0443\u0447\u0438, \u0434\u043e\u0434\u0430\u0439\u0442\u0435 \u043f\u0440\u0435\u0444\u0456\u043a\u0441 \u0456 \u0441\u0443\u0444\u0456\u043a\u0441 <code>%s<\/code>\u0437 <code>%%<\/code>, \u0456 \u0432\u0441\u0435 \u0431\u0443\u0434\u0435 \u0433\u043e\u0442\u043e\u0432\u043e.<\/p>\n<p><div id=\"PostUnique_PostSource\" style=\"padding-top: 50px\">\u0414\u0436\u0435\u0440\u0435\u043b\u043e \u0437\u0430\u043f\u0438\u0441\u0443:  <a target=\"_blank\" rel=\"noopener nofollow\" href=\"\/\/tommcfarlin.com\" class=\"external external_icon\">tommcfarlin.com<\/a><\/div><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u041e\u0431\u0440\u043e\u0431\u043a\u0430 sprintf \u0456 LIKE \u0443 T-SQL \u043f\u043e\u0442\u0440\u0435\u0431\u0443\u0454 \u043f\u0435\u0432\u043d\u043e\u0457 \u0441\u0442\u0440\u0430\u0442\u0435\u0433\u0456\u0447\u043d\u043e\u0457 \u043e\u0431\u0440\u043e\u0431\u043a\u0438, \u0456 \u043e\u0441\u044c \u044f\u043a \u0446\u0435 \u0437\u0440\u043e\u0431\u0438\u0442\u0438.<\/p>\n","protected":false},"author":1,"featured_media":163659,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","_wp_rev_ctl_limit":""},"categories":[869,849,725],"tags":[1174],"class_list":["post-230291","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-wordpress-10","category-pidruchnyky","category-rozrobnyk","tag-affiai-uk"],"_links":{"self":[{"href":"https:\/\/wordpress.mediadoma.com\/uk\/wp-json\/wp\/v2\/posts\/230291","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wordpress.mediadoma.com\/uk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wordpress.mediadoma.com\/uk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wordpress.mediadoma.com\/uk\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wordpress.mediadoma.com\/uk\/wp-json\/wp\/v2\/comments?post=230291"}],"version-history":[{"count":0,"href":"https:\/\/wordpress.mediadoma.com\/uk\/wp-json\/wp\/v2\/posts\/230291\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/wordpress.mediadoma.com\/uk\/wp-json\/wp\/v2\/media\/163659"}],"wp:attachment":[{"href":"https:\/\/wordpress.mediadoma.com\/uk\/wp-json\/wp\/v2\/media?parent=230291"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wordpress.mediadoma.com\/uk\/wp-json\/wp\/v2\/categories?post=230291"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wordpress.mediadoma.com\/uk\/wp-json\/wp\/v2\/tags?post=230291"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}