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"');
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"');
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.
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();
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! :)