# Database values by ID

Use this shortcode to fetch a value from a specific column in a MySQL table, based on a matching key in another column.\
You’ll need to specify both the column to retrieve data from and the column to search against using the provided key.

For example, the shortcode below executes the corresponding SQL query:

{% code overflow="wrap" %}
```
[sv slug="db-value-by-id" table="wp_users" column="user_login" column-to-search="id" key="3" key-format="%d"]
```
{% endcode %}

_Pseudo MySQL:_

```
Select {column} from {table} where {column-to-search} = {key}
```

_Actual MySQL:_

```
Select user_login from wp_users where id=3
```

**Shortcode Arguments**

The shortcode supports the following arguments:

| Argument          | Description                                                                              | Options                                         | Example                                                                                                                                                  |
| ----------------- | ---------------------------------------------------------------------------------------- | ----------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------- |
| table             | MySQL table to search                                                                    | text                                            | \[sv slug="db-value-by-id" table="wp\_users" column="user\_login" column-to-search="id" key="3" key-format="%d"]                                         |
| cache             | If enabled, cache the results of the SQL query for the number of seconds specified by    | True (default) or False                         | \[sv slug="db-value-by-id" table="wp\_users" column="user\_login" column-to-search="id" key="3" key-format="%d" cache=false]                             |
| cache-duration    | Number of seconds the data should be cached for (before hitting the database again)      | Number, default 3600 seconds (1 hour)           | \[sv slug="db-value-by-id" table="wp\_users" column="user\_login" column-to-search="id" key="3" key-format="%d" cache=true cache-duration=60]            |
| column            | MySQL table column to return                                                             | text                                            | \[sv slug="db-value-by-id" table="wp\_users" column="user\_login" column-to-search="id" key="3" key-format="%d"]                                         |
| column-to-search  | MySQL column to use in Where clause                                                      | text                                            | \[sv slug="db-value-by-id" table="wp\_users" column="user\_login" column-to-search="id" key="3" key-format="%d"]                                         |
| key               | Value to compare against column-to-search                                                | text                                            | \[sv slug="db-value-by-id" table="wp\_users" column="user\_login" column-to-search="id" key="3" key-format="%d"]                                         |
| key-format        | To stop SQL injection, a format (either numeric or string) must be specified for the key | Either "%d" (for a number) or "%s" for a string | \[sv slug="db-value-by-id" table="wp\_users" column="user\_login" column-to-search="id" key="3" key-format="%d"]                                         |
| message-not-found | Message to display if no value can be found for the given key.                           | Either blank or text                            | \[sv slug="db-value-by-id" table="wp\_users" column="user\_login" column-to-search="id" key="3" key-format="%d" message-not-found="Could not find user"] |

**Enabling and disabling this shortcode**

You can enable this shortcode in the WordPress admin by navigating to **Snippet Shortcodes > Settings**.\
Then, set the **“Enable ‘db-value-by-id’ shortcode?”** option to **Yes**.

The following filter can also be used to force the setting to always be "No".

{% code lineNumbers="true" %}
```
add_filter( 'disable-ss-sc-db-value-by-id', function( $default ){ 
    return true; 
});
```
{% endcode %}
