0

I want to include a variable in my sql statement but it's not outputting any data. How do I include a variable correctly?

$vStyles = $wpdb->get_results( 'SELECT slug from vf_venuestyles WHERE vid = "$vid"');

Raj
  • 65
  • 1
  • 9

3 Answers3

1

Change the quote marks around: $vStyles = $wpdb->get_results("SELECT slug from vf_venuestyles WHERE vid = '$vid'");

But, unless that function / class already does something magic about that, you should look into using something with proper prepared statements.

junkfoodjunkie
  • 3,168
  • 1
  • 19
  • 33
0

The best way is to use prepared statement

$stmt = $dbh->prepare("INSERT INTO REGISTRY (name, value) VALUES (:name, :value)");
$stmt->bindParam(':name', $name);
$stmt->bindParam(':value', $value);

// insert line
$name = 'one';
$value = 1;
$stmt->execute();
sheplu
  • 2,937
  • 3
  • 24
  • 21
  • he's apparently using WordPress, I don't think it uses PDO. Is there a WP way to do prepared queries? – Barmar Aug 15 '17 at 00:45
0

Variables only get executed if they are inside double quotes.

Simply swap your quotation marks like this:

$vStyles = $wpdb->get_results("SELECT slug from vf_venuestyles WHERE vid = '$vid'");

Or combine the query with the variable, like this, remembering to also include the secondary quotation marks:

$vStyles = $wpdb->get_results('SELECT slug from vf_venuestyles WHERE vid = '" . $vid . '"');

Hope this helps! :)

Obsidian Age
  • 41,205
  • 10
  • 48
  • 71