I have the following database structure and I want to select everything from a specific user and month. However it does return the specific user data but all the months.
The month i selected in this example is 2014-02 or February 2014
date |user | some more data |
----------------------------------------
2014-01-20 |user1 | more data | //will not be skipped
2014-02-01 |user1 | more data |
2014-02-01 |user2 | more data | //will be skipped
2014-02-02 |user1 | more data |
2014-05-02 |user1 | more data | //will not be skipped
I call the following query via a prepared statement function. The LIKE operator seems like it isn't working.
$query = "SELECT * FROM table1 WHERE user = ? AND date LIKE ?"
$user = "user1"; //These 2 actually come from user input so:
$date = "2014-02%"; //$user = $username; $date = $y."-".$m."%";
//function that runs the prepared statement (simplified)
//this function works for all other queries.
$result = $database->runQuery($query, array($user, $date));
while($row = mysqli_fetch_array($result){
echo $row['date'];
echo $row['user'];
echo $row['some more data'];
echo "<br>";
}
This prints
2014-01-20 user1 more data //this line should not be printed but it does.
2014-02-01 user1 more data
2014-02-02 user1 more data
2014-05-02 user1 more data //also should not be printed.
So my question is what am I doing wrong with the LIKE operator?
My actual query.
//The system holds phone call data.
//dcontext is a variable that decides in which column the user is found.
//in case of dcontext = outgoing calls
//clid = username + extensions
//src = users phone number
//dst = to phone number (this can be extension from another user in the system)
//which wont be registered double in this case.
//when dcontext = incomming calls
//clid and src = the caller ID/phone number from the person calling.
//dst = the users phone number.
//when dcontext = internal call transfer
//clid = username + extension
//src = users phone number
//dst = transfered number
"SELECT * FROM table WHERE
(dcontext = ? AND (clid = ? OR src = ? OR dst = ? OR dst = ?)) OR
(dcontext = ? AND dst = ?) OR
(dcontext = ? AND (clid = ? OR src = ?)) AND
date LIKE ?"
date type = DateTime