Jump to content

PHP Variable in a MySQLi Query


MNSarahG

Recommended Posts

Hi,

 

I'm trying to get this SQLi query to return entries that have the current month as their value in my database's "annMonth" field. 

 

The idea is that it returns a list of employees who have an anniversary in the current month.  I'm having issues with (I think) my syntax, and a few hours of experimenting has yielded nothing.  When I manually replace the variable in the WHERE clause with a "3" (instead of '$currDate;') it works fine. 

 

My code:

$currDate = date("m");

/* Send a query to the server */
if ($result = mysqli_query($link, '
SELECT firstName, lastName, annMonth, annDay, annYear 
FROM employees 
WHERE annMonth = '$currDate;'
ORDER BY annDay')) {

 

Thanks in advance for any help!

 

- Sarah -

Link to comment
https://forums.phpfreaks.com/topic/94970-php-variable-in-a-mysqli-query/
Share on other sites

Hi there. Your single quotes around the variable are breaking the query, since it's surrounded by single quotes itself. Use double quotes (") for the query:

 

$currDate = date("m");

/* Send a query to the server */
if ($result = mysqli_query($link, "
SELECT firstName, lastName, annMonth, annDay, annYear 
FROM employees 
WHERE annMonth = '$currDate;'
ORDER BY annDay")) {

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.