Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

i am having a slight problem with passing php-variables to a stroed procedure call from a php-script. Here comes the details:

This is how it works very perfect - passing the params as string:

$myHTML = $wpdb->query( 'CALL show_average_time_spent(2, "2011-10-24", "2011-10-24", @myHTML)' );

Brings back a perfect result.

And this is how it not works and throws the error (passing params as variables):

$date_from = '2011-10-24';
$date_to = '2011-10-27';
$myHTML = $wpdb->query( 'CALL show_average_time_spent(2, $date_from, $date_to, @myHTML)' );

throws this error:

WordPress database error: [Unknown column '$date_from' in 'field list'] CALL show_average_time_spent(2, $date_from, $date_to, @myHTML)

The $date_from is only used in this php-script for holding the selected date.

i realy appreciate any kind of help with this issue.

Thanks to all of you trying to help solving this problem.

Cheers, Joe

share|improve this question
    
Btw: You should show us the actual procedure and yes, it's off topic + close vote. –  kaiser Feb 6 '12 at 7:20
add comment

1 Answer

up vote 0 down vote accepted

Please take a look at the Codex to see how to prepare your statement:

// Example straight copy-paste from Codex
$metakey    = "Harriet's Adages";
$metavalue  = "WordPress' database interface is like Sunday Morning: Easy.";

$wpdb->query( $wpdb->prepare( 
    "
        INSERT INTO $wpdb->postmeta
        ( post_id, meta_key, meta_value )
        VALUES ( %d, %s, %s )
    ", 
    10, 
    $metakey, 
    $metavalue 
) );

Try this with your code too (it's about security).

share|improve this answer
    
Hello kaiser, you are my hero. It works fine now with this method. Thanks so much. Have a nice day, Joe. P.S.: if you ever will come to bangkok, i owe you some beers. –  JSS Feb 6 '12 at 8:25
    
n/p, you're welcome. Could you please hit the check mark below the voting arrows to mark it as solution and close the question? And if I make it to Bangkok, then I'll remind you 'bout that beer :) –  kaiser Feb 6 '12 at 9:46
2  
I think it's probably worth noting, though, that the reason that the original query was failing was that you were using single quotes (') instead of double quotes ("). Text within single quotes is not parsed, it is just outputted directly. Text within double quotes is parsed. So what you would have wanted is 'CALL show_average_time_spent(2, '.$date_from.', '.$date_to.', @myHTML)'. That said, @kaiser's solution is the RIGHT way to do things. –  m0r7if3r Feb 6 '12 at 16:20
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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