Calcetines Extreme

Calcetines Extreme
Take care of you using the best socks

Thursday, December 3, 2020

How to connect to Wordpress SQL using shortcuts

Problem:

Yo need to access to any database table connected to your wordpress or maybe an external one to extract any information and process it.

Solution:

Use this TestSQLand add is to your wordpress functions.php of "Code Snipper" plugin:

function TestSQL_Function( $atts ) {
$link = mysqli_connect("localhost", "user", "****", "DATABASENAME");
$sql = "SELECT * FROM table";
if($result = mysqli_query($link, $sql)){
    if(mysqli_num_rows($result) > 0){
        while($row = mysqli_fetch_array($result)){
            $line = $line.$row['fielname1']."(".$row['fieldname2'].")";
        }
return $line;
        // Free result set
        mysqli_free_result($result);
    } else{
        return "No records matching your query were found.";
    }
} else{
    return "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
// Close connection
mysqli_close($link);
}
add_shortcode( 'TestSQL', 'TestSQL_Function' );

Now, you only need to add the wordpress shortcut [TestSQL] in your wordpres post or page.