I'm trying to make a very abstract PDO select function, but for some reason, I'm getting an empty array returned.
function listAllProducts () {
return select('products', '', '');
}
function select( $tableName, $property, $value ){
switch($tableName)
{
case "products":
$tbl = 'products';
break;
case "cart":
$tbl = 'cart';
break;
}
switch($property){
case "id":
$property = 'id';
break;
case "name":
$property = 'name';
break;
}
$db = new PDO('mysql:host=localhost;dbname=ecom;charset=utf8', 'root', 'PASSWORD');
if( strcmp($value, '') != 0 ){ //if $value is not empty
$query = "SELECT * FROM $tbl WHERE $property = :value";
$stmt = $db->prepare($query);
//bind params
$stmt->bindValue(':value', $value);
}
else{
$query = "SELECT * FROM $tbl";
$stmt = $db->prepare($query);
}
//execute
try{
$stmt->execute();
$result = $stmt->fetchAll();
return $result;
}
catch(Exception $e){
return $e;
}
}
Is there something simple I'm missing?
EDIT: Thanks for the help, but after adding a switch and removing table and columns from my bound params, I'm still getting an empty result set :(
EDIT 2: I've found a somewhat convoluted solution, added it above, but it looks like it's working for now! More testing is necessary.