0

I have following code:

<?php
$q = intval($_GET['q']);

global $wpdb;


$myoption =  get_option( 'fixformdata_options' );
$myoptionValue = maybe_unserialize( $myoption );    

$result2 = $wpdb->get_row
(
    $wpdb->prepare
    (
        "SELECT * FROM {$myoptionValue['tablename']} WHERE personeelsNummer = %d", $q
    )
);

if($result2) {
  echo  json_encode( $result2 );
}

?>

I get the following error:

SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data
var parsed = JSON.parse(response);

There seems to be something wrong with the returned data, but I don't know what...

Help greatly appreciated!

6
  • Have you looked at the response itself? Without actually looking at the data, you can't know what the issue is.
    – Otto
    Aug 28 '14 at 12:20
  • Yeah i was just doing this, it seems that get_option doesn't get called... at first it was working properly, I have no clue why it isn't working now Aug 28 '14 at 12:24
  • What context is this code in? Are you sure WordPress is loaded? Do you check the $myoption variable first? Do you have debugging/error reporting enabled? Aug 28 '14 at 12:30
  • -How can I be sure that Wordpress is loaded? I don't know what that means, I mean, I can get on my website and everythings loads fine. -Why do I need to check the $myoption? Also how do I do this? -Yes Aug 28 '14 at 12:35
  • Where does the code in your question exist? A file of its own? In a plugin? In your theme? Aug 28 '14 at 15:02
1

Check out the codex on wpdb. In your case, get_row returns the first row of the result set as an object. There's no need for any mysqli_* functions when using $wpdb.

if ( $result2 )
    echo json_encode( $result2 );

And a heads up for SQL injection - use the prepare method:

$wpdb->get_row(
    $wpdb->prepare(
        "SELECT * FROM {$myoptionValue['tablename']} WHERE personeelsNummer = %s",
        $q
    )
);

If personeelsNummer is an integer, use the placeholder %d instead of %s

1
  • I updated my code, it still doesn't work ... Aug 28 '14 at 12:05

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.