How to Run MySQL Queries in WordPress

How to Run MySQL Queries in WordPress

At some point in your WordPress development career you may be presented with the need to write some custom mysql queries but how to do? don’t worry i write basic MySQL queries these may help you.

WordPress MySQL Select Query

How to fetch single row in WordPress

$ures = $wpdb->get_row("SELECT * FROM `custome_users` ORDER BY `id` DESC");
$ures ->name;

How to fetch multiple rows in WordPress

$ures = $wpdb->get_results("SELECT * FROM `custome_users` ORDER BY `id` DESC");					
foreach ($ures as $uresfet)
{
    $uresfet->name;
}

How to count total number of rows in WordPress

 $ures = $wpdb->get_results("SELECT * FROM `custome_users` ORDER BY `id` DESC");
 $rows=$wpdb->num_rows;

How to delete row from table using MySQL query in WordPress

 $wpdb->query("DELETE FROM custome_users where id='$deleteid'");

How to update data into database using MySQL query in WordPress

 $wpdb->query("UPDATE `sample_requests` SET `firstname` = '$firstname', `lastname` = '$lastname' WHERE `id` ='$id'");
About Author

My name is Mukesh Jakhar and I am a Web Application Developer and Software Developer, currently living in Jaipur, India. I have a Master of Computer Application in Computer Science from JNU Jaipur University. I loves to write on technology and programming topics. Apart from this, I love to travel and enjoy the beauty of nature.

Sign up for weekly update

Milkshake is almost ready. If you're interested in testing it out, then sign up below to get exclusive access.