M
M
mrWan2017-09-07 01:18:27
JavaScript
mrWan, 2017-09-07 01:18:27

What's similar to the WHERE 1 condition that can be used in a prepared PDO query?

$select = "SELECT type FROM tablepart WHERE 1";
 
    if (empty($foo1) && empty($foo2)) {
        $where = '??';  // пробовал так: $where = ':true'; $placeholders[':true'] = ' AND true'; ничего не вышло
                $placeholders[??]// вот сюда нужно засунуть 
    }
    elseif (empty($foo2) && !empty($foo1)) {
        $where = " AND foo1= :foo1";
        $placeholders[':foo1] = $foo1;
    }
    elseif (!empty($foo1) && !empty($foo2)) {
        $where = ' AND foo1= :foo1 AND foo2 = :foo2';
        $placeholders[':foo1'] = $foo1;
        $placeholders[':foo2'] = $foo2;
    }
    $sql = $select . $where; 
    $stmt = $pdo->prepare($sql);
    $stmt->execute($placeholders);
    $data = $stmt->fetchAll();

Of course, you can check the $where variable for emptiness here, and decide whether to concatenate it or not to the rest of the sql query.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
A
Alexey Ukolov, 2019-05-19
@luemerg

You can hide the input into which something was loaded and instead show a new empty one.

but then I found out that it is impossible to change the value of the input file field.
You can’t change it, but when submitting the form, adding these files to FormData by hand is completely.

I
Ilya Chermoshentsev, 2019-05-19
@webgrill


$total = count($_FILES['photo']['name']);
for( $i=0 ; $i < $total ; $i++ ) {
$tmpFilePath = $_FILES['photo']['tmp_name'][$i];
if ($tmpFilePath != ""){
$newFilePath = "./uploadFiles/" . $_FILES['photo']['name'][$i];
if(move_uploaded_file($tmpFilePath, $newFilePath)) {
//Handle other code here
}
}
}

F
FanatPHP, 2017-09-07
@FanatPHP

As correctly noted above, you don't need a "pen for grade 8": it works exactly the sameWHERE 1 in PDO . Another thing is that everything can be done much easier, and without where 1, and without a bunch of conditions (imagine your code if the query will involve not two fields, but three). In fact, no more conditions are needed than fields in the request :

if (!empty($foo1)) {
    $where = "foo1= :foo1";
    $placeholders[':foo1'] = $foo1;
}
if (!empty($foo2)) {
    $where = 'foo2 = :foo2';
    $placeholders[':foo2'] = $foo2;
}
$sql = "SELECT type FROM tablepart";
if ($where)
{
    $sql .= " WHERE ".implode(" AND ", $where);
}
$stmt = $pdo->prepare($sql);
$stmt->execute($placeholders);
$data = $stmt->fetchAll();

A
Alexander Aksentiev, 2017-09-07
@Sanasol

what about the meaning? works the same way.
through prepare, in any case, it will escape the lines and nothing will turn out.
so leave where 1 or where 1=1
and calmly add or not add conditions through and.
But it's even better to take a normal sql builder/orm etc. who will do it all on his own.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question