T-ez Lopchan T-ez Lopchan - 1 year ago 74
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'

Answer Source

Try This:

$query ="SELECT * FROM '$tbl_name' WHERE id='".$condition['id']."' AND anycondition='".$condition['anycondition']."'";
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download