Search code examples
phpmysqlmedoo

Medoo php sql - change search term


i am using the "medoo"-php/mysql-class (http://www.medoo.in) for my latest project. I like the quiet easy way to work with my SQL-stuff.

But i wondered if it is possible to change the search term according to an option from an input-select-form. Lets say we got search-option "user by name", we would go with:

$data = $database->select("account", [
"user_name",
"user_id",
"phone",
"email",
"age"], [
"user_name" => $_POST['user_name']]);

Fine so far. Now we get the search option "user by ID". Every thing else stays just the same. So i need only the

["user_name" => $_POST['user_name']]);

to be like

["user_id" => $_POST['user_id']]);

Is there a way to change that without writing the whole statement again?

It is just a example. Moreover for my project i will need to change other options of the query like update to insert oder different join-options. But for the moment i am fine with an answer to that.

Anyway, what do you think about the Medoo-Class. Got any cool alternative Class-solutions for me? Basically i know how to work with SQL querys but getting stuff to the array at the end is always driving me crazy. So i would love to get stuff faster and easier with a class.

Thx a lot for your help! Best, Lox


Solution

  • You can just change the $where array before running the query like that:

    if ($type == 'id')
    {
        $where = ["user_id" => $_POST['user_id']];
    }
    
    if ($type == 'user_name')
    {
        $where = ["user_name" => $_POST['user_name']];
    }
    
    $data = $database->select("account",
        [
            "user_name",
            "user_id",
            "phone",
            "email",
            "age"
        ],
        $where
    );