0

I write plugin and use in my code this function: mysqli_multi_query($wpdb->dbh, $sql_trigger);. It's not hard to guess that on some hosting systems this function works correctly, but on some it produces the following error:

Warning: mysqli_multi_query() expects parameter 1 to be mysqli, resource given in /home/blah/blah.com/www/wp-content/plugins/blah/blah.php on line xxx

Question: how do I determine which type of database is used on the hosting and then correctly execute my query to the database?

Notice: My query to the database contains the following code:

CREATE TRIGGER `blah_set_current_time_and_date_values`
BEFORE INSERT
ON $table_name
    FOR EACH ROW BEGIN
        SET NEW.date_visited = DATE_FORMAT(NOW(), '%d-%m-&Y');
        SET NEW.time_visited = TIME_FORMAT(NOW(), '%H:%i:%s');
END;

That's why I decided to use mysqli_multi_query(), since I do not know how else to execute this query. Also, I tried use $wpdb->query($sql_string);, but in this case WP returned error: [Trigger already exists]. Although it does not exactly exist at that time, I delete it manually using DROP TRIGGER IF EXISTS wordpress_rezart_statistics_set_current_time_and_date_values‌​.

  • how is it even remotely specific to wordpress? – Mark Kaplun Jul 8 '17 at 5:14
  • @MarkKaplun, Obviously, I'm looking for the most native way to do what in WP. – Stanislav Belichenko Jul 8 '17 at 5:47
  • since you do not want to use wordpress DB api, I am not sure what kind of "native" you expect here – Mark Kaplun Jul 8 '17 at 8:24
  • I want, but I can't: wordpress.stackexchange.com/questions/127344/… Also, I tried use $wpdb->query($sql_string);, but in this case WP returned error: [Trigger already exists]. Although it does not exactly exist at that time, I delete it manually using DROP TRIGGER IF EXISTS wordpress_rezart_statistics_set_current_time_and_date_values. – Stanislav Belichenko Jul 8 '17 at 8:37
1

wpdb->dbh is declared as protected property, you should not even have access to it, unless you are subclassing wpdb?

The only public property is wpdb->is_mysql which is just hardcoded for true and meant for detection of non–native database drop–ins.

So overall wpdb context is meant to be internal, you set up the connection and let it deal with the detail from that point.

I have no experience with triggers in WP context, but it sounds strange for arbitrary query to not just work in query() method. I would recommend to get to the bottom of that. It might be that issue is with query or something related and not WP call running it.

  • Thx for you answer. 'is declared as protected property, you should not even have access to it, unless you are subclassing wpdb' - I just read about this method there: wordpress.stackexchange.com/a/127351/122529 – Stanislav Belichenko Jul 17 '17 at 8:24
  • 'I would recommend to get to the bottom of that. It might be that issue is with query or something related and not WP call running it' - yeah, I just had a hope that someone had already encountered triggers. Right now I do simple - hide_errors() / show_errors() and it works perfectly :) – Stanislav Belichenko Jul 17 '17 at 8:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.