Wordpress:发布任何帖子时触发一个功能

I have written a function and want to make it trigger when any post is published on my website.

I have tried everything, but still doesn't work. So, I post the following code, just in case somebody helps with it. :)

function run_when_post_published(){
        $mysqli = new mysqli("localhost", "root", "", "wp");
        .blablabla

            $mysqli->close();

    }
    add_action('publish_post', 'run_when_post_published');

Can you tell what's wrong? Thanks again. :)

as mentioned in WordPress doc :

publish_post is an action triggered whenever a post is updated and its new status is "publish".

publish_post Reference

as i understand from your question you need to trigger an function whenever post has been added regardless of post status so here is the correct hook to use

function run_when_post_published( $post_id, $post, $update){
 $mysqli = new mysqli("localhost", "root", "", "wp");
  .blablabla
     $mysqli->close();

}
add_action('save_post', 'run_when_post_published', 10, 3);

save_post Reference