{"id":3108,"date":"2025-03-22T00:12:50","date_gmt":"2025-03-21T16:12:50","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=3108"},"modified":"2025-03-22T00:12:50","modified_gmt":"2025-03-21T16:12:50","slug":"php-mysql-where-%e5%ad%90%e5%8f%a5","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/03\/22\/php-mysql-where-%e5%ad%90%e5%8f%a5\/","title":{"rendered":"PHP MySQL WHERE \u5b50\u53e5"},"content":{"rendered":"\n<p>\u5728 PHP \u4e2d\u4e0e MySQL \u4ea4\u4e92\u65f6\uff0c<strong>WHERE \u5b50\u53e5<\/strong> \u7528\u4e8e\u7b5b\u9009\u7b26\u5408\u6761\u4ef6\u7684\u8bb0\u5f55\u3002\u5b83\u5e38\u5e38\u4e0e <code>SELECT<\/code>\u3001<code>UPDATE<\/code>\u3001<code>DELETE<\/code> \u8bed\u53e5\u4e00\u8d77\u4f7f\u7528\uff0c\u901a\u8fc7\u6307\u5b9a\u6761\u4ef6\u6765\u9650\u5236\u67e5\u8be2\u3001\u66f4\u65b0\u6216\u5220\u9664\u7684\u6570\u636e\u8303\u56f4\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>1. WHERE \u5b50\u53e5\u7684\u57fa\u672c\u8bed\u6cd5<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT column1, column2, ...\nFROM table_name\nWHERE condition;<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>condition<\/strong>\uff1a\u6307\u5b9a\u6761\u4ef6\u8868\u8fbe\u5f0f\uff0c\u53ea\u6709\u6ee1\u8db3\u6761\u4ef6\u7684\u8bb0\u5f55\u624d\u4f1a\u88ab\u9009\u4e2d\u3002<\/li>\n\n\n\n<li><strong>column1, column2<\/strong>\uff1a\u8981\u67e5\u8be2\u7684\u5217\u3002<\/li>\n\n\n\n<li><strong>table_name<\/strong>\uff1a\u6570\u636e\u8868\u7684\u540d\u79f0\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u5728 PHP \u4e2d\uff0c\u901a\u5e38\u901a\u8fc7 <code>MySQLi<\/code> \u6216 <code>PDO<\/code> \u6765\u6267\u884c\u5e26\u6709 <code>WHERE<\/code> \u5b50\u53e5\u7684\u67e5\u8be2\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>2. \u4f7f\u7528 MySQLi WHERE \u5b50\u53e5<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2.1 \u4f7f\u7528\u9762\u5411\u5bf9\u8c61\u65b9\u5f0f<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n$servername = \"localhost\";\n$username = \"root\";\n$password = \"\";\n$dbname = \"my_database\";\n\n\/\/ \u521b\u5efa\u8fde\u63a5\n$conn = new mysqli($servername, $username, $password, $dbname);\n\n\/\/ \u68c0\u67e5\u8fde\u63a5\nif ($conn-&gt;connect_error) {\n    die(\"\u8fde\u63a5\u5931\u8d25: \" . $conn-&gt;connect_error);\n}\n\n\/\/ SQL \u67e5\u8be2\uff0c\u5e26\u6709 WHERE \u5b50\u53e5\n$sql = \"SELECT id, username, email FROM users WHERE username = 'john_doe'\";\n$result = $conn-&gt;query($sql);\n\n\/\/ \u68c0\u67e5\u7ed3\u679c\nif ($result-&gt;num_rows &gt; 0) {\n    while($row = $result-&gt;fetch_assoc()) {\n        echo \"ID: \" . $row&#91;\"id\"]. \" - \u7528\u6237\u540d: \" . $row&#91;\"username\"]. \" - \u90ae\u7bb1: \" . $row&#91;\"email\"]. \"&lt;br&gt;\";\n    }\n} else {\n    echo \"\u6ca1\u6709\u627e\u5230\u8bb0\u5f55\";\n}\n\n\/\/ \u5173\u95ed\u8fde\u63a5\n$conn-&gt;close();\n?&gt;<\/code><\/pre>\n\n\n\n<p>\ud83d\udccc <strong>\u89e3\u6790<\/strong>\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>WHERE \u5b50\u53e5<\/strong>\uff1a\u5728\u67e5\u8be2\u4e2d\u6307\u5b9a\u4e86 <code>username = 'john_doe'<\/code> \u7684\u6761\u4ef6\uff0c\u53ea\u6709 <code>username<\/code> \u4e3a <code>'john_doe'<\/code> \u7684\u8bb0\u5f55\u4f1a\u88ab\u8fd4\u56de\u3002<\/li>\n\n\n\n<li>\u4f7f\u7528 <strong><code>fetch_assoc()<\/code><\/strong> \u83b7\u53d6\u6bcf\u884c\u6570\u636e\uff0c\u663e\u793a\u67e5\u8be2\u7ed3\u679c\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2.2 \u4f7f\u7528\u8fc7\u7a0b\u5316\u65b9\u5f0f<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n$servername = \"localhost\";\n$username = \"root\";\n$password = \"\";\n$dbname = \"my_database\";\n\n\/\/ \u521b\u5efa\u8fde\u63a5\n$conn = mysqli_connect($servername, $username, $password, $dbname);\n\n\/\/ \u68c0\u67e5\u8fde\u63a5\nif (!$conn) {\n    die(\"\u8fde\u63a5\u5931\u8d25: \" . mysqli_connect_error());\n}\n\n\/\/ \u6267\u884c\u67e5\u8be2\uff0c\u5e26\u6709 WHERE \u5b50\u53e5\n$sql = \"SELECT id, username, email FROM users WHERE username = 'john_doe'\";\n$result = mysqli_query($conn, $sql);\n\n\/\/ \u68c0\u67e5\u7ed3\u679c\nif (mysqli_num_rows($result) &gt; 0) {\n    while($row = mysqli_fetch_assoc($result)) {\n        echo \"ID: \" . $row&#91;\"id\"]. \" - \u7528\u6237\u540d: \" . $row&#91;\"username\"]. \" - \u90ae\u7bb1: \" . $row&#91;\"email\"]. \"&lt;br&gt;\";\n    }\n} else {\n    echo \"\u6ca1\u6709\u627e\u5230\u8bb0\u5f55\";\n}\n\n\/\/ \u5173\u95ed\u8fde\u63a5\nmysqli_close($conn);\n?&gt;<\/code><\/pre>\n\n\n\n<p>\ud83d\udccc <strong>\u89e3\u6790<\/strong>\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u540c\u6837\u4f7f\u7528 <code>WHERE<\/code> \u5b50\u53e5\u6765\u7b5b\u9009\u6570\u636e\uff0c<code>username = 'john_doe'<\/code> \u4f5c\u4e3a\u67e5\u8be2\u6761\u4ef6\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>3. \u4f7f\u7528 PDO WHERE \u5b50\u53e5<\/strong><\/h2>\n\n\n\n<p>PDO \u63d0\u4f9b\u4e86\u66f4\u4e3a\u5b89\u5168\u7684\u65b9\u5f0f\uff0c\u652f\u6301\u9884\u5904\u7406\u8bed\u53e5\uff0c\u9632\u6b62 SQL \u6ce8\u5165\u653b\u51fb\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3.1 \u4f7f\u7528 PDO \u9884\u5904\u7406\u8bed\u53e5\u4e0e WHERE \u5b50\u53e5<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n$servername = \"localhost\";\n$username = \"root\";\n$password = \"\";\n$dbname = \"my_database\";\n\ntry {\n    \/\/ \u521b\u5efa PDO \u8fde\u63a5\n    $pdo = new PDO(\"mysql:host=$servername;dbname=$dbname\", $username, $password);\n    \/\/ \u8bbe\u7f6e\u9519\u8bef\u6a21\u5f0f\n    $pdo-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);\n\n    \/\/ \u51c6\u5907 SQL \u67e5\u8be2\uff0c\u5e26\u6709 WHERE \u5b50\u53e5\n    $stmt = $pdo-&gt;prepare(\"SELECT id, username, email FROM users WHERE username = :username\");\n    $stmt-&gt;bindParam(':username', $username);  \/\/ \u7ed1\u5b9a\u53c2\u6570\n\n    \/\/ \u8bbe\u7f6e\u53c2\u6570\u5e76\u6267\u884c\n    $username = \"john_doe\";\n    $stmt-&gt;execute();\n\n    \/\/ \u68c0\u67e5\u7ed3\u679c\n    if ($stmt-&gt;rowCount() &gt; 0) {\n        \/\/ \u8f93\u51fa\u6bcf\u4e00\u884c\u6570\u636e\n        while ($row = $stmt-&gt;fetch(PDO::FETCH_ASSOC)) {\n            echo \"ID: \" . $row&#91;'id'] . \" - \u7528\u6237\u540d: \" . $row&#91;'username'] . \" - \u90ae\u7bb1: \" . $row&#91;'email'] . \"&lt;br&gt;\";\n        }\n    } else {\n        echo \"\u6ca1\u6709\u627e\u5230\u8bb0\u5f55\";\n    }\n} catch (PDOException $e) {\n    echo \"\u9519\u8bef: \" . $e-&gt;getMessage();\n}\n\n\/\/ \u5173\u95ed\u8fde\u63a5\n$pdo = null;\n?&gt;<\/code><\/pre>\n\n\n\n<p>\ud83d\udccc <strong>\u89e3\u6790<\/strong>\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u9884\u5904\u7406\u8bed\u53e5<\/strong>\uff1a\u4f7f\u7528 <code>prepare()<\/code> \u521b\u5efa SQL \u67e5\u8be2\u8bed\u53e5\uff0c<code>bindParam()<\/code> \u7ed1\u5b9a\u53c2\u6570\uff0c\u786e\u4fdd\u5b89\u5168\u6267\u884c\u67e5\u8be2\u3002<\/li>\n\n\n\n<li><strong><code>:username<\/code><\/strong>\uff1a\u5728\u67e5\u8be2\u4e2d\u4f7f\u7528\u5360\u4f4d\u7b26\uff08\u53c2\u6570\u5316\u67e5\u8be2\uff09\u4ee3\u66ff\u76f4\u63a5\u62fc\u63a5\u53d8\u91cf\uff0c\u9632\u6b62 SQL \u6ce8\u5165\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>4. WHERE \u5b50\u53e5\u5e38\u89c1\u7528\u6cd5<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4.1 \u4f7f\u7528\u591a\u4e2a\u6761\u4ef6\uff08AND \/ OR\uff09<\/strong><\/h3>\n\n\n\n<p>\u4f60\u53ef\u4ee5\u901a\u8fc7 <code>AND<\/code> \u6216 <code>OR<\/code> \u6765\u7ed3\u5408\u591a\u4e2a\u6761\u4ef6\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$sql = \"SELECT id, username, email FROM users WHERE username = 'john_doe' AND email = 'john@example.com'\";<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>AND<\/strong>\uff1a\u4e24\u4e2a\u6761\u4ef6\u90fd\u5fc5\u987b\u4e3a <code>TRUE<\/code>\u3002<\/li>\n\n\n\n<li><strong>OR<\/strong>\uff1a\u4efb\u610f\u4e00\u4e2a\u6761\u4ef6\u4e3a <code>TRUE<\/code>\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4.2 \u4f7f\u7528\u901a\u914d\u7b26\uff08LIKE\uff09<\/strong><\/h3>\n\n\n\n<p><code>LIKE<\/code> \u7528\u4e8e\u6a21\u7cca\u67e5\u8be2\uff0c\u53ef\u4ee5\u914d\u5408 <code>%<\/code> \u6765\u4f7f\u7528\u901a\u914d\u7b26\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$sql = \"SELECT id, username, email FROM users WHERE username LIKE '%john%'\";<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>%<\/code><\/strong>\uff1a\u8868\u793a\u4efb\u610f\u6570\u91cf\u7684\u5b57\u7b26\u3002<\/li>\n\n\n\n<li><strong><code>_<\/code><\/strong>\uff1a\u8868\u793a\u5355\u4e2a\u5b57\u7b26\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4.3 \u4f7f\u7528\u8303\u56f4\uff08BETWEEN\uff09<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>$sql = \"SELECT id, username, email FROM users WHERE id BETWEEN 1 AND 10\";<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>BETWEEN<\/code><\/strong>\uff1a\u9009\u62e9\u5728\u6307\u5b9a\u8303\u56f4\u5185\u7684\u8bb0\u5f55\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4.4 \u4f7f\u7528\u6392\u5e8f\uff08ORDER BY\uff09<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>$sql = \"SELECT id, username, email FROM users WHERE username LIKE '%john%' ORDER BY id DESC\";<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>ORDER BY<\/code><\/strong>\uff1a\u6307\u5b9a\u7ed3\u679c\u6392\u5e8f\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4.5 \u4f7f\u7528\u7a7a\u503c\uff08IS NULL\uff09<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>$sql = \"SELECT id, username, email FROM users WHERE email IS NULL\";<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>IS NULL<\/code><\/strong>\uff1a\u7b5b\u9009\u5b57\u6bb5\u503c\u4e3a\u7a7a\u7684\u8bb0\u5f55\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u603b\u7ed3<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>WHERE \u5b50\u53e5<\/strong> \u662f\u67e5\u8be2\u4e2d\u6700\u5e38\u7528\u7684\u8fc7\u6ee4\u6761\u4ef6\uff0c\u901a\u8fc7\u6307\u5b9a\u6761\u4ef6\u6765\u9650\u5b9a\u67e5\u8be2\u7684\u6570\u636e\u8303\u56f4\u3002<\/li>\n\n\n\n<li>\u5728 PHP \u4e2d\u4f7f\u7528 <strong>MySQLi<\/strong> \u6216 <strong>PDO<\/strong> \u53ef\u4ee5\u6267\u884c\u5e26\u6709 WHERE \u5b50\u53e5\u7684 SQL \u67e5\u8be2\uff0c\u786e\u4fdd\u67e5\u8be2\u6570\u636e\u7684\u7cbe\u786e\u6027\u3002<\/li>\n\n\n\n<li>\u4f7f\u7528 <strong>\u9884\u5904\u7406\u8bed\u53e5<\/strong> \u548c <strong>\u53c2\u6570\u5316\u67e5\u8be2<\/strong> \u53ef\u4ee5\u9632\u6b62 SQL \u6ce8\u5165\uff0c\u63d0\u9ad8\u6570\u636e\u5e93\u64cd\u4f5c\u7684\u5b89\u5168\u6027\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 PHP \u4e2d\u4e0e MySQL \u4ea4\u4e92\u65f6\uff0cWHERE \u5b50\u53e5 \u7528\u4e8e\u7b5b\u9009\u7b26\u5408\u6761\u4ef6\u7684\u8bb0\u5f55\u3002\u5b83\u5e38\u5e38\u4e0e SELECT\u3001UPD [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3109,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[80],"tags":[],"class_list":["post-3108","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3108","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=3108"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3108\/revisions"}],"predecessor-version":[{"id":3110,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3108\/revisions\/3110"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/3109"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=3108"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=3108"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=3108"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}