Support » Developing with WordPress » How to insert binary strings via $wpdb interface?

  • Hi, is there a way to insert a binary string via $wpdb interface?
    It looks like strip_invalid_text() is called by insert() and prepare(), so my binary string is always stripped off.
    My case is that my table has a uuid column which is defined as binary(16) for compactness. I know changing it to varchar(36) would work, but I’d love to know if there is a way to work around that before making the change.

    Thanks.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    It appears $wpdb doesn’t consider binary as a valid format. It only accepts float, integer, string. Your only option is to use PHP mysqli_* functions to interact with your table.

    Dion

    (@diondesigns)

    OK, let’s try again…

    The WordPress DBAL is designed for newbies who don’t understand SQL basics. That is good (I suppose) in some ways, but limits its usefulness for those who are more advanced. Thankfully the DBAL has a backdoor that allows more advanced developers to completely bypass $wpdb and use mysqli procedural functions.

    First you should make sure mysqli is being used. I put code similar to the following in my activation functions:

    global $wpdb;
    
    if (!extension_loaded('mysqli')) {
    	wp_die('mysqli extension not available. Please upgrade your version of PHP.');
    }
    else if (!$wpdb->__get('use_mysqli')) {
    	wp_die("Please add <strong><code>define('WP_USE_EXT_MYSQL', false);</code></strong> to your wp-config.php file.");
    }

    At this point you can be assured that the mysqli extension is being used. In the code that initializes the plugin (or in a theme’s functions.php file), add the following lines:

    global $dbh;
    $dbh = $wpdb->__get('dbh');

    You can now use mysqli procedural functions as needed by supplying the $dbh variable for the DB handle.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘How to insert binary strings via $wpdb interface?’ is closed to new replies.