{"id":869,"date":"2016-01-21T17:16:43","date_gmt":"2016-01-21T08:16:43","guid":{"rendered":"http:\/\/n8finch.com\/?p=869"},"modified":"2016-08-25T10:59:34","modified_gmt":"2016-08-25T01:59:34","slug":"sql-database-queries-and-foreach","status":"publish","type":"post","link":"https:\/\/n8finch2024.local\/sql-database-queries-and-foreach\/","title":{"rendered":"This Week I Learned: SQL database queries and foreach()"},"content":{"rendered":"

It’s been a busy week.<\/p>\n

I’m putting together a new theme (to be unveiled soon), updating some projects I’ve been working on, and tuned my guitar for the first time in about three months.<\/p>\n

I’ve come across a\u00a0couple new things I wanted to record.\u00a0It’s funny how, when putting sites together, you come across things you could have used a couple projects ago. Seems to always happen.<\/p>\n

So, here’s to trying to stay ahead of the curve…<\/p>\n

SQL Queries in phpMyAdmin<\/h2>\n

One of my recent projects needed to be built fairly quickly and was quite data-intensive.\u00a0There was a massive migration of data, and it was iterated, which means that we added things about five or seven times, occasionally with different metadata.\u00a0We also\u00a0allowed for some new characters to be used in data that was previously filtered out.<\/p>\n

Needless to say, the data needed to be updated. Using phpMyAdmin, I was able to do this fairly quickly with some quieres:<\/p>\n

To update user login names:<\/p>\n

UPDATE wp_users SET user_login\u00a0= 'NewUserName' WHERE\u00a0user_login\u00a0= 'OldUserName';<\/pre>\n

To update usermeta fields:<\/p>\n

UPDATE wp_usermeta SET meta_key = 'NewMetaKey' WHERE meta_key = 'OldMetaKey';\r\n\r\nUPDATE wp_usermeta SET meta_value = 'NewMetaValue' WHERE meta_value = 'OldMetaValue';<\/pre>\n

There was plenty of\u00a0information to change and these queries made it all possible.<\/p>\n

foreach() Loop Syntax<\/h2>\n

I’ve been following WPMUDev’s series on PHP. It’s been pretty good so far. Actually, this article from Daniel Pataki<\/a> has clarified something I’ve been struggling with: the foreach() loop.<\/p>\n

The basic loop can look something like this:<\/p>\n

\r\n<pre>$names = array( "Nate Finch", "Someone Else", "Another Somone" );\r\necho "<\/pre>\r\n<ul>"; foreach( $names as $name ) { echo "\r\n\t<li>" . $name . "<\/li>\r\n<\/ul>\r\n"; } echo "\r\n<ul>\r\n<ul>";<\/ul>\r\n<\/ul>\r\n\/\/Don't include the pre-tags\r\n<\/pre>\n

I was always confused about the syntax, but knowing that $name<\/code> and $names<\/code> can be whatever variables you want, and then where to call those parameters (in a list, for example), it all just clicked.<\/p>\n

It works the same when going through an associative array as well, something like: foreach( $details as $label =&gt; $value )<\/code>, where $details<\/code> is the name of the array, $label<\/code> is the key, and $value<\/code> is the key’s value.<\/p>\n

I’m sure this syntax is almost exactly the same for Javascript (if I’m remembering correctly).<\/p>\n

So, for the Learner I am, this has been a very good week.<\/p>\n

###<\/p>\n

Cheers,<\/p>\n

Nate<\/p>\n","protected":false},"excerpt":{"rendered":"

It’s been a busy week. I’m putting together a new theme (to be unveiled soon), updating some projects I’ve been working on, and tuned my guitar for the first time in about three months. I’ve come across a\u00a0couple new things I wanted to record.\u00a0It’s funny how, when putting sites together, you come across things you […]<\/p>\n","protected":false},"author":1,"featured_media":973,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_eb_attr":"","footnotes":""},"categories":[9,1,8],"tags":[],"yoast_head":"\nThis Week I Learned: SQL database queries and foreach() | Nate Finch<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/n8finch-site.site.strattic.io\/sql-database-queries-and-foreach\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"This Week I Learned: SQL database queries and foreach() | Nate Finch\" \/>\n<meta property=\"og:description\" content=\"It’s been a busy week. I’m putting together a new theme (to be unveiled soon), updating some projects I’ve been working on, and tuned my guitar for the first time in about three months. I’ve come across a\u00a0couple new things I wanted to record.\u00a0It’s funny how, when putting sites together, you come across things you […]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/n8finch-site.site.strattic.io\/sql-database-queries-and-foreach\/\" \/>\n<meta property=\"og:site_name\" content=\"Nate Finch\" \/>\n<meta property=\"article:published_time\" content=\"2016-01-21T08:16:43+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2016-08-25T01:59:34+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/n8finch-site.site.strattic.io\/wp-content\/uploads\/2020\/11\/sql-queries-and-foreach-loops_uevls1.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"650\" \/>\n\t<meta property=\"og:image:height\" content=\"433\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Nate\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@n8finch\" \/>\n<meta name=\"twitter:site\" content=\"@n8finch\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Nate\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/n8finch-site.site.strattic.io\/sql-database-queries-and-foreach\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/n8finch-site.site.strattic.io\/sql-database-queries-and-foreach\/\"},\"author\":{\"name\":\"Nate\",\"@id\":\"https:\/\/n8finch.local\/#\/schema\/person\/619068414583ca3c43bb135af49a47ce\"},\"headline\":\"This Week I Learned: SQL database queries and foreach()\",\"datePublished\":\"2016-01-21T08:16:43+00:00\",\"dateModified\":\"2016-08-25T01:59:34+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/n8finch-site.site.strattic.io\/sql-database-queries-and-foreach\/\"},\"wordCount\":443,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/n8finch.local\/#\/schema\/person\/619068414583ca3c43bb135af49a47ce\"},\"articleSection\":[\"20 Hours Ahead\",\"Blog\",\"Web Development\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/n8finch-site.site.strattic.io\/sql-database-queries-and-foreach\/\",\"url\":\"https:\/\/n8finch-site.site.strattic.io\/sql-database-queries-and-foreach\/\",\"name\":\"This Week I Learned: SQL database queries and foreach() | Nate Finch\",\"isPartOf\":{\"@id\":\"https:\/\/n8finch.local\/#website\"},\"datePublished\":\"2016-01-21T08:16:43+00:00\",\"dateModified\":\"2016-08-25T01:59:34+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/n8finch-site.site.strattic.io\/sql-database-queries-and-foreach\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/n8finch-site.site.strattic.io\/sql-database-queries-and-foreach\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/n8finch-site.site.strattic.io\/sql-database-queries-and-foreach\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/n8finch.local\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"This Week I Learned: SQL database queries and foreach()\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/n8finch.local\/#website\",\"url\":\"https:\/\/n8finch.local\/\",\"name\":\"Nate Finch\",\"description\":\"All the things, keeping it clever...🤓\",\"publisher\":{\"@id\":\"https:\/\/n8finch.local\/#\/schema\/person\/619068414583ca3c43bb135af49a47ce\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/n8finch.local\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/n8finch.local\/#\/schema\/person\/619068414583ca3c43bb135af49a47ce\",\"name\":\"Nate\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/n8finch.local\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/n8finch2024.local\/wp-content\/uploads\/2020\/11\/nate-pic_ykd6mq.jpg\",\"contentUrl\":\"https:\/\/n8finch2024.local\/wp-content\/uploads\/2020\/11\/nate-pic_ykd6mq.jpg\",\"width\":796,\"height\":792,\"caption\":\"Nate\"},\"logo\":{\"@id\":\"https:\/\/n8finch.local\/#\/schema\/person\/image\/\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"This Week I Learned: SQL database queries and foreach() | Nate Finch","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/n8finch-site.site.strattic.io\/sql-database-queries-and-foreach\/","og_locale":"en_US","og_type":"article","og_title":"This Week I Learned: SQL database queries and foreach() | Nate Finch","og_description":"It’s been a busy week. I’m putting together a new theme (to be unveiled soon), updating some projects I’ve been working on, and tuned my guitar for the first time in about three months. I’ve come across a\u00a0couple new things I wanted to record.\u00a0It’s funny how, when putting sites together, you come across things you […]","og_url":"https:\/\/n8finch-site.site.strattic.io\/sql-database-queries-and-foreach\/","og_site_name":"Nate Finch","article_published_time":"2016-01-21T08:16:43+00:00","article_modified_time":"2016-08-25T01:59:34+00:00","og_image":[{"width":650,"height":433,"url":"https:\/\/n8finch-site.site.strattic.io\/wp-content\/uploads\/2020\/11\/sql-queries-and-foreach-loops_uevls1.jpg","type":"image\/jpeg"}],"author":"Nate","twitter_card":"summary_large_image","twitter_creator":"@n8finch","twitter_site":"@n8finch","twitter_misc":{"Written by":"Nate","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/n8finch-site.site.strattic.io\/sql-database-queries-and-foreach\/#article","isPartOf":{"@id":"https:\/\/n8finch-site.site.strattic.io\/sql-database-queries-and-foreach\/"},"author":{"name":"Nate","@id":"https:\/\/n8finch.local\/#\/schema\/person\/619068414583ca3c43bb135af49a47ce"},"headline":"This Week I Learned: SQL database queries and foreach()","datePublished":"2016-01-21T08:16:43+00:00","dateModified":"2016-08-25T01:59:34+00:00","mainEntityOfPage":{"@id":"https:\/\/n8finch-site.site.strattic.io\/sql-database-queries-and-foreach\/"},"wordCount":443,"commentCount":0,"publisher":{"@id":"https:\/\/n8finch.local\/#\/schema\/person\/619068414583ca3c43bb135af49a47ce"},"articleSection":["20 Hours Ahead","Blog","Web Development"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/n8finch-site.site.strattic.io\/sql-database-queries-and-foreach\/","url":"https:\/\/n8finch-site.site.strattic.io\/sql-database-queries-and-foreach\/","name":"This Week I Learned: SQL database queries and foreach() | Nate Finch","isPartOf":{"@id":"https:\/\/n8finch.local\/#website"},"datePublished":"2016-01-21T08:16:43+00:00","dateModified":"2016-08-25T01:59:34+00:00","breadcrumb":{"@id":"https:\/\/n8finch-site.site.strattic.io\/sql-database-queries-and-foreach\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/n8finch-site.site.strattic.io\/sql-database-queries-and-foreach\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/n8finch-site.site.strattic.io\/sql-database-queries-and-foreach\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/n8finch.local\/"},{"@type":"ListItem","position":2,"name":"This Week I Learned: SQL database queries and foreach()"}]},{"@type":"WebSite","@id":"https:\/\/n8finch.local\/#website","url":"https:\/\/n8finch.local\/","name":"Nate Finch","description":"All the things, keeping it clever...🤓","publisher":{"@id":"https:\/\/n8finch.local\/#\/schema\/person\/619068414583ca3c43bb135af49a47ce"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/n8finch.local\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/n8finch.local\/#\/schema\/person\/619068414583ca3c43bb135af49a47ce","name":"Nate","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/n8finch.local\/#\/schema\/person\/image\/","url":"https:\/\/n8finch2024.local\/wp-content\/uploads\/2020\/11\/nate-pic_ykd6mq.jpg","contentUrl":"https:\/\/n8finch2024.local\/wp-content\/uploads\/2020\/11\/nate-pic_ykd6mq.jpg","width":796,"height":792,"caption":"Nate"},"logo":{"@id":"https:\/\/n8finch.local\/#\/schema\/person\/image\/"}}]}},"_links":{"self":[{"href":"https:\/\/n8finch2024.local\/wp-json\/wp\/v2\/posts\/869"}],"collection":[{"href":"https:\/\/n8finch2024.local\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/n8finch2024.local\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/n8finch2024.local\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/n8finch2024.local\/wp-json\/wp\/v2\/comments?post=869"}],"version-history":[{"count":23,"href":"https:\/\/n8finch2024.local\/wp-json\/wp\/v2\/posts\/869\/revisions"}],"predecessor-version":[{"id":1135,"href":"https:\/\/n8finch2024.local\/wp-json\/wp\/v2\/posts\/869\/revisions\/1135"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/n8finch2024.local\/wp-json\/wp\/v2\/media\/973"}],"wp:attachment":[{"href":"https:\/\/n8finch2024.local\/wp-json\/wp\/v2\/media?parent=869"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/n8finch2024.local\/wp-json\/wp\/v2\/categories?post=869"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/n8finch2024.local\/wp-json\/wp\/v2\/tags?post=869"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}