MySQL WHERE clause

时间:2015-07-28 15:59:06

标签: php mysql where implode

Is it possible to have a WHERE clause after imploding array? I need to insert only rows where priority >=1. Thanks.

$array = array(); 
foreach ($priority as $priority) 
$array[] = "('$id', '$studentname', '$title', '$academicdiscipline', '$priority')";

$query = "INSERT INTO flux_project_selection (id, studentname, title,
academicdiscipline, priority)  VALUES ". implode(',', $array);

1 个答案:

答案 0 :(得分:2)

Insert statements shouldn't have a where clause. Instead use PHP to filter what goes into the $array variable. Here's an example:

<?php

$array = array(); 
foreach ($priority as $priority) {
    if ($priority >=1) {
        $array[] = "('$id', '$studentname', '$title', '$academicdiscipline', '$priority')"; 
    }
}

$query = "INSERT INTO flux_project_selection (id, studentname, title,
academicdiscipline, priority)  VALUES ". implode(',', $array);

?>