0

I have a problem regarding the bind_param() function. So I know it's important to prepare and parameterize your query, but I've come to a problem.

Below is how I usually do the preparation, and it works well

function select($what, $table, $id, $age)
{
    $sql = 'SELECT ' . $what . ' FROM ' . $table . ' WHERE id = ? AND age = ?';

    $stmt = $mysqli->prepare($sql);
    $stmt->bind_param('ii', $id, $age);
    $stmt->execute();
    $result = $stmt->get_result();

    return $result;
}

Now, what I want to achieve but I don't know how

function select($what, $table, $where)
{
    $sql = 'SELECT ' . $what . ' FROM ' . $table . ' WHERE ' . $where;

    $stmt = $mysqli->prepare($sql);
    $stmt->bind_param(); #Now what?
    $stmt->execute();
    $result = $stmt->get_result();

    return $result;
}

The called function will look like

select('*', 'tablename', 'id = 1, age = 25, height = 1.5, dob = 01/01/1990');

I can and will clarify my question if it's not clear enough already. Thank you in advance!

Yattsu
  • 11
  • 3
  • This has been asked million times. E.g http://stackoverflow.com/questions/5100046/how-to-bind-mysqli-bind-param-arguments-dynamically-in-php – u_mulder Apr 25 '17 at 20:17
  • Also http://stackoverflow.com/questions/755036/what-is-a-simple-solution-for-dynamic-mysqli-bind-param-arguments-in-php – u_mulder Apr 25 '17 at 20:18
  • Thank you for your answer @u_mulder . This is what I was looking for. – Yattsu Apr 25 '17 at 20:29

0 Answers0