0

Hello I have been developing in PHP and now am just starting the transition to Wordpress. In PHP I have this ->

 // Prepare statement
    $sqlstatement = $conn->prepare("INSERT INTO importtest (id, area, country, city, town, postcode, 
        lotarea, price, bedrooms, bathrooms, summary, latitude, longlitude, street, streetno, type, image) 
        VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
        // Bind the parameters I want (datatype, parameters)
    $sqlstatement->bind_param('sssssssssssssssss', $ID, $area, $country, $city, $town, $postcode, $lotarea, $price, 
                         $bedrooms, $bathrooms, $summary, $latitude, $longlitude, $street, 
                         $streetno, $name, $image);

    // Execute
    $sqlstatement->execute();

I notice that the insert for wpdb takes arrays as in the column name, the data and the data type. My question is does Wordpress have an equivalent for binding parameters as I'm sure can't just insert variables?

2

Short answer, no. WPDB is a class that's been around for a while (back when WP used to support PHP 4), and lacks a lot of features PHP5 drivers offer, including parameter binding.

As you mentioned, wpdb::insert is the closest you can get to your original code:

$wpdb->insert(
    'importtest',
    array(
        'id'      => $ID,
        'area'    => $area,
        'country' => $country,
    ),
    array(
        '%d',
        '%s',
        '%s',
    )   
);

It uses wpdb::prepare to escape the data, which works in a sprintf-type fashion, supporting integers (%d), floats (%f) and strings (%s):

$wpdb->prepare( "SELECT * FROM importtest WHERE area = %s", ' "foobar" ' );
// SELECT * FROM importtest WHERE area = " \"foobar\" "

Note that, like other driver prepare methods, there's no need to quote the placeholder.

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.