I want to select all from my workloaddata Table, where the Sched_Days field has at least one similar day. With Days selected by the USER(via checkbox). Which the values are stored in $wwdays variable.
I've converted my $wwdays into an array by doing $daysIn = explode(',', $wwdays, -1);
Now, I'm using $daysIn as a parameter in my WHERE clause, but my query is returning a false result. Is there something wrong with my syntax?
/*
$wwdays has this string value "Monday, Tuesday, Wednesday, Saturday,"
I've used $daysIn = explode(',', $wwdays, -1); To get just the days
And used print_r($daysIn); to check the details, I have this result
Array ( [0] => Monday [1] => Tuesday [2] => Wednesday [3] => Saturday )
So I assume that my $daysIn variable is an array with the days as its element.
*/
$daysIn = explode(',', $wwdays, -1);
$connect = mysqli_connect("localhost", "root", "", "smis");
$sql = "SELECT * FROM workloaddata
WHERE Sched_Days IN (".implode(',',$daysIn).")
";
$result = mysqli_query($connect, $sql);
while($row = mysqli_fetch_array($result))
{
// do things ..
}
// I'm having mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\
</div>
You must do something like bellow (take in attention on comments):
// Connect to DB
$connect = mysqli_connect("localhost", "root", "", "smis");
$daysIn = explode(',', $wwdays);
// Clear white-spaces and prevent sql injection
$daysIn = array_map(function($item) use ($connect) {
return mysqli_real_escape_string($connect, trim($item));
}, $daysIn);
// Remove empty items
$daysIn = array_filter($daysIn);
if (!empty($daysIn)) {
// Wrap "" on condition values
$sql = 'SELECT * FROM `workloaddata` WHERE `Sched_Days` IN ("'.implode('","',$daysIn).'") ';
$result = mysqli_query($connect, $sql);
while($row = mysqli_fetch_array($result))
{
// do things ..
}
}
But it is recommended to use PDO and bind params instead of putting directly into query.