Skip to content
David Kittell
David Kittell

Application & System: Development / Integration / Orchestration

  • Services
    • Application Development
    • Online Application Integration
  • Code
  • Online Tools
  • Tech Support
David Kittell

Application & System: Development / Integration / Orchestration

MySQL Procedure Get WordPress Site Name

Posted on October 13, 2015October 13, 2015 By David Kittell

If you manage many WordPress site databases it can get a bit crazy to know which site goes with what database but it gets more “fun” when you have a network site, below I attempt to help document some ways to help.

This first example is a rather long process that doesn’t seem to make since why you would do it until you get further down.

DROP PROCEDURE IF EXISTS GetSiteNameFromDefinedTable;

DELIMITER $$
CREATE PROCEDURE GetSiteNameFromDefinedTable (tableName VARCHAR(30))
BEGIN
	SET @s = CONCAT (
			'SELECT CASE
                    WHEN option_name = ''blogname''
                        THEN option_value
                    END AS Site_Name FROM '
			,tableName
			,' WHERE option_name = ''blogname'''
			);
	PREPARE stmt
	FROM @s;
	EXECUTE stmt;
	DEALLOCATE PREPARE stmt;
END $$
DELIMITER;
call GetSiteNameFromDefinedTable('wp_options');

Complete Process, this is long and likely could be cleaner but works.

DROP PROCEDURE IF EXISTS GetSiteNameFromDefinedTable;

DELIMITER $$
CREATE PROCEDURE GetSiteNameFromDefinedTable (tableName VARCHAR(30))
BEGIN
	SET @s = CONCAT ('INSERT INTO SiteNameDetails (Site_Name,Site_URL, Site_Table, Site_Database)
		SELECT distinct
			(
            SELECT CASE
                    WHEN option_name = ''blogname''
                        THEN option_value
                    END
            FROM ',tableName,'
            WHERE option_name = ''blogname''
            ) AS Site_Name,
            
            (
            SELECT CASE
                    WHEN option_name = ''siteurl''
                        THEN option_value
                    END
            FROM ',tableName,'
            WHERE option_name = ''siteurl''
            ) AS ''URL'',
            ''',tableName,''' AS Table_Name,
            Database() AS Database_Name
		FROM ',tableName);
	PREPARE stmt
	FROM @s;
	EXECUTE stmt;
	DEALLOCATE PREPARE stmt;
END $$
DELIMITER ;

#call GetSiteNameFromDefinedTable('wp_2_options');

DROP PROCEDURE IF EXISTS SiteNameDefinedTables;
DELIMITER $$
CREATE PROCEDURE SiteNameDefinedTables()
BEGIN
  DECLARE done BOOLEAN DEFAULT FALSE;
  DECLARE _id varchar(50);
  DECLARE cur CURSOR FOR (SELECT DISTINCT table_name FROM `information_schema`.`columns` WHERE table_name LIKE '%_options%') ;
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET done := TRUE;
  
  OPEN cur;
  
  testLoop: LOOP
    FETCH cur INTO _id;
    IF done THEN
      LEAVE testLoop;
    END IF;
     
    CALL GetSiteNameFromDefinedTable(_id);
   
   
  END LOOP testLoop;
  
  CLOSE cur;
  
END
$$
DELIMITER ;

Once you have the procedures created you can do this

drop temporary table if exists SiteNameDetails;
Create temporary table if not exists SiteNameDetails(
Site_Name VarChar(200),
Site_URL VarChar(200),
Site_Table VarChar(100),
Site_Database VarChar(100)
);

call SiteNameDefinedTables();

SELECT 
    wpb.blog_id,
    wpb.site_id,
    snd.Site_Name,
    snd.Site_URL,
    snd.Site_Table,
    snd.Site_Database
FROM
    SiteNameDetails snd
        LEFT OUTER JOIN
    wp_blogs wpb ON CONCAT(snd.site_url, '/') = CONCAT('http://', wpb.domain, wpb.path)
ORDER BY site_id , blog_id;
All information on this site is shared with the intention to help. Before any source code or program is ran on a production (non-development) system it is suggested you test it and fully understand what it is doing not just what it appears it is doing. I accept no responsibility for any damage you may do with this code.

Related

Code MySQL SQL WordPress WordPress SQL

Post navigation

Previous post
Next post

Related Posts

Azure RM – Remove Azure RM PowerShell Modules

Posted on March 7, 2018

foreach ($module in (Get-Module -ListAvailable AzureRM*).Name |Get-Unique) { write-host "Removing Module $module" Uninstall-module $module } All information on this site is shared with the intention to help. Before any source code or program is ran on a production (non-development) system it is suggested you test it and fully understand what…

Read More

Azure MSSQL – Server Firewall Rules

Posted on July 14, 2017

Run these commands on the master table. First get the existing rules SELECT * FROM sys.firewall_rules ORDER BY name; Based on the above query if there are any rules you need to remove get the name and replace Client with the appropriate name EXECUTE sp_delete_firewall_rule @name = N’Client’ — https://docs.microsoft.com/en-us/sql/relational-databases/system-stored-procedures/sp-delete-firewall-rule-azure-sql-database…

Read More

AES Encrypted SSO

Posted on March 12, 2015March 31, 2017

Passing information between one site and another is becoming more and more common but how can you do it without exposing confidential data? Below are some examples of how you can encrypt text and then decrypt it on the other end. C# to C# <%@ Page Language="C#" AutoEventWireup="true" CodeFile="AES_Encryption.aspx.cs" Inherits="AES_Encryption"…

Read More

Code

Top Posts & Pages

  • PowerShell - Rename Pictures to Image Taken
  • Front Page
  • C# - Start/Stop/Restart Services
  • MacPorts / HomeBrew - Rip CD tracks from terminal
  • PowerShell - Show File Extensions

Recent Posts

  • Javascript – Digital Clock with Style
  • BASH – Web Ping Log
  • BASH – Picture / Video File Name Manipulation
  • Mac OSX Terminal – Create SSH Key
  • Bash – Rename Picture

Top Posts

  • PowerShell - Rename Pictures to Image Taken
  • C# - Start/Stop/Restart Services
  • MacPorts / HomeBrew - Rip CD tracks from terminal
  • PowerShell - Show File Extensions
  • Open On Screen Keyboard (OSK)
  • SQLite - Auto-Increment / Auto Generate GUID
©2025 David Kittell | WordPress Theme by SuperbThemes