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
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")) {

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.