{"id":56,"date":"2024-02-29T22:22:01","date_gmt":"2024-02-29T22:22:01","guid":{"rendered":"https:\/\/www.kwwd.co.uk\/blog\/?p=56"},"modified":"2024-02-29T22:24:17","modified_gmt":"2024-02-29T22:24:17","slug":"change-image-filenames-in-mysql-jpg-to-png","status":"publish","type":"post","link":"https:\/\/www.kwwd.co.uk\/blog\/change-image-filenames-in-mysql-jpg-to-png\/","title":{"rendered":"Change Image Filenames In MYSQL (jpg to png)"},"content":{"rendered":"<p>Create a script (replacing the variables as necessary) and run it to replace ALL instances of (in this case) &#8220;star.jpg&#8221; to &#8220;star.png&#8221;<\/p>\n<pre><code class=\"language-php line-numbers\">\r\n&lt;?php\r\n\r\n\/\/ CHANGE THESE VARIABLES TO MATCH YOUR SERVER SETTINGS\r\n$DB_Host = 'localhost';\r\n$DB_Name = 'Your_Database_Name';\r\n$DB_User = 'Database_User_Name';\r\n$DB_Password = 'Your_Database_Password';\r\n\r\n\/\/ What Do You Want To Change?\r\n\r\n$OldValue = 'star.jpg';\r\n$NewValue = 'star.png';\r\n\r\n\/\/ ** DONE EDITING ** \/\/\r\n\r\n\/\/ Connect to your WordPress database\r\n$conn = mysqli_connect($DB_Host, $DB_User, $DB_Password, $DB_Name);\r\n\r\n\/\/ Check connection\r\nif (!$conn) {\r\n  die(\"Connection failed: \" . mysqli_connect_error());\r\n}\r\n\r\n\/\/ Escape the image filenames for safe SQL query usage\r\n$old_image = mysqli_real_escape_string($conn, $OldValue);\r\n$new_image = mysqli_real_escape_string($conn, $NewValue);\r\n\r\n\/\/ Construct the update query\r\n$sql = \"UPDATE `your_table_name` SET `post_content` = REPLACE(`post_content`, '$old_image', '$new_image')\";\r\n\r\n\/\/ Execute the query\r\nif (mysqli_query($conn, $sql)) {\r\n  echo \"Image URL updated successfully.\";\r\n} else {\r\n  echo \"Error updating image URL: \" . mysqli_error($conn);\r\n}\r\n\r\n\/\/ Close the connection\r\nmysqli_close($conn);\r\n\r\n?&gt;\r\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Create a script (replacing the variables as necessary) and run it to replace ALL instances of (in this case) &#8220;star.jpg&#8221; to &#8220;star.png&#8221; &lt;?php \/\/ CHANGE [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[29],"tags":[31,32,24,30],"class_list":["post-56","post","type-post","status-publish","format-standard","hentry","category-mysql","tag-database","tag-filename","tag-mysql","tag-string-maniplation-replace"],"_links":{"self":[{"href":"https:\/\/www.kwwd.co.uk\/blog\/wp-json\/wp\/v2\/posts\/56","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kwwd.co.uk\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kwwd.co.uk\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kwwd.co.uk\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kwwd.co.uk\/blog\/wp-json\/wp\/v2\/comments?post=56"}],"version-history":[{"count":0,"href":"https:\/\/www.kwwd.co.uk\/blog\/wp-json\/wp\/v2\/posts\/56\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.kwwd.co.uk\/blog\/wp-json\/wp\/v2\/media?parent=56"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kwwd.co.uk\/blog\/wp-json\/wp\/v2\/categories?post=56"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kwwd.co.uk\/blog\/wp-json\/wp\/v2\/tags?post=56"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}