Hi there,
Please can someone help me!
I have recently been learning oop PHP. I have created a class for my database, which basically makes my life easier when doing queries.

I have created a "where" method which basically when used will append to the select statement.
For example:
//class I have created
$query = new MySqlDb;

//this is the where statement
$query->DbWhere("LULevel='10' && LUId='200'");

//db select statement
$res = $query->DbSelect("*", "login","ORDER BY LUFName ASC");

Now the above code works absolutely fine but the value for the where method is: "LULevel='10' && LUId='200'".

I want to neaten it up so I only need to enter the property and the value like this: "$query->DbWhere("LULevel","10","LUId", "200");" so I can use mysql_real_escape_string just on the value etc.

This is fine when it comes to maybe inputting one set of property and value like: ("LUId","200") but if you were to add more like:("LULevel","10","LUId", "200","LUName","bob") then I need the code to work out that there is more property and values which then gets appended to the select statement in a correct way.

I know that there is a way of maybe using array or array_key and combining it with for each loops etc but I can't work it out.
I have built a test function below which is on the same lines of what my method in the class does.

//test function
function where($prop, $val){
  $where = array();
  $where[$prop] = $val;
  return $where;
}
print_r(where("LUName","bob","LUId","200"));

If you test the above function code, it will only print the first set:
//output
Array ( [LUName] => bob )

So just to reiterate I want to be able to get all the property and values and not just the first one.

Hope someone can help!

Thanks
Jat

Hi Jat,

You could put your 'props' and 'vals' into an array and use foreach to add those to your WHERE statement. This is how I mean:

$props = array("LUName", "LUId");
$vals = array("bob", "200");
foreach($props as $key => $prop){
  print(where($prop,$vals[$key]));
}
Member Avatar for diafol

This looks like a lot of work just to write a where clause. This method will not be very flexible as you can't apply operators like AND,OR,XOR,NOT, =, <>, !=, >,>=,<, <=, BETWEEN, IN etc etc. If you need something a little more complex than ... WHERE x = y AND a = b AND m = n ..., this can cause more work that it's worth.

Also, you need to FIX the datatypes to a certain parameter position, otherwise you'll encode/hash/clean the wrong parameters. Personally, I'd clean everything.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.