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?

1
  • You'll probably find most of the information you want and need on the Class Reference - wpdb codex page. Commented Jan 11, 2015 at 22:20

1 Answer 1

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 and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.