0

I am trying to learn how to do a simple Select query using $wpdb-prepare.

I am checking wether an email submitted by the user is already available in the database.

There has to be something wrong with the syntax because the string placeholder is undefined, according to PHPstorm.

$mail = $_POST['email'];
$table = $wpdb->prefix . 'members';
$sql = $wpdb->prepare( "SELECT * FROM $table WHERE email = $s", $mail);
$result = $sql->get_results($sql);

What would the correct syntax be?

  • 2
    Placeholder should be %s and not $s. Latter is a variable, not a placeholder, that is why you get the undefined notice – Pieter Goosen Oct 26 '15 at 12:43
1

You need to define $wpdb as global.

Try this code.

global $wpdb;

$mail = $_POST['email'];

$table = $wpdb->prefix . 'members';

$result = $sql->get_results( "SELECT * FROM $table WHERE email = %s", $mail);
  • I had "global $wpdb;" at the top, just forgot to include it. But the problem was with $s (should be %s) – Galivan Oct 26 '15 at 13:12

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.