{"id":622,"date":"2021-11-14T20:27:57","date_gmt":"2021-11-15T01:27:57","guid":{"rendered":"https:\/\/molecularsciences.org\/content\/?p=622"},"modified":"2024-02-08T08:45:34","modified_gmt":"2024-02-08T13:45:34","slug":"escaping-string-before-inserting-into-mysql","status":"publish","type":"post","link":"https:\/\/molecularsciences.org\/content\/escaping-string-before-inserting-into-mysql\/","title":{"rendered":"Escaping string before inserting into MySQL"},"content":{"rendered":"\n<p>If a string contains a single quote, it will generate an error. mysqli-&gt;real_escape_string() escapes quotes for MySQL. See example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n    \/\/ create database connection\n    $conn = new mysqli($HOST, $USER, $PASS, $DB);\n    if ($conn-&gt;connect_error) {\n      die (\"Connection failed: \" . $conn-&gt;connect_error);\n    }\n\n    \/\/ this string will break SQL if not escaped \n    $line = \"John's car\"\n\n    \/\/ escape strings for mysql\n    $line = $conn-&gt;real_escape_string($line);\n\n    \/\/ insert query      \n    $sql = \"INSERT INTO `mydb`.`lines` (`lineid`, `line`) VALUES (NULL, '{$line}');\";\n    if ($conn-&gt;query($sql) === TRUE) {\n      \/\/ query successful\n    } else {\n      echo \"Error inserting data: \" . $conn-&gt;error . \"\\n\";\n    }\n\n    \/\/ close database connection\n    mysqli_close($conn);\n?&gt;<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>If a string contains a single quote, it will generate an error. mysqli-&gt;real_escape_string() escapes quotes for MySQL. See example:<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16,204],"tags":[],"class_list":["post-622","post","type-post","status-publish","format-standard","hentry","category-database","category-mysql"],"_links":{"self":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/622","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/comments?post=622"}],"version-history":[{"count":1,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/622\/revisions"}],"predecessor-version":[{"id":623,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/622\/revisions\/623"}],"wp:attachment":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/media?parent=622"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/categories?post=622"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/tags?post=622"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}