T-ez Lopchan T-ez Lopchan - 6 days ago 4x
MySQL Question

Adding content of Array into where Condition in SQL query

I'm trying to make this function get() that takes tbl_name and condition which will be used in WHERE.

Here $condition is an array like:

$condition = array(
'id' => 'some id',
'anycondition' => 'any value');

public function get($tbl_name, $condition = null) {
if($condition) {
$data = "select * from '$tbl_name' WHERE '$condition'";
else {
$data = $tbl_name;
return $data;

I want the echo be like this

select * from $tbl_name WHERE id='some id' and anycondition='any value'


Try This:

$query ="SELECT * FROM '$tbl_name' WHERE id='".$condition['id']."' AND anycondition='".$condition['anycondition']."'";