Skip to content

Instantly share code, notes, and snippets.

@thewasta
Last active December 10, 2018 00:27
Show Gist options
  • Save thewasta/f0f3fc58b82ecd2c0225dc9567a85270 to your computer and use it in GitHub Desktop.
Save thewasta/f0f3fc58b82ecd2c0225dc9567a85270 to your computer and use it in GitHub Desktop.
Select from DB with a condicional WHERE
<?php
/**
* Created by Schenier Aldair Lopez
* Date: 06/12/18
* Time: 0:53
*
*/
function select($tabla,$datos=null, $item=null){
if ($datos!=null){
$data = implode(",",$datos);
}
if ($datos === null){
$query = "SELECT * FROM $tabla";
}
if ($item!=null){
if (count($item)<=1){
$valor = [];
foreach ($item as $key=>$value) {
$columna[]="\"".$value."\"";
$valor[]=$key;
}
$valores = implode($valor);
$columnas= implode($columna);
$query="SELECT $data FROM $tabla WHERE $valores=$columnas";
}
}
if ($datos!=null && $item ==null){
$query="SELECT $data FROM $tabla";
}
$stmt = Conexion::conectar()->prepare($query);
$stmt->setFetchMode(PDO::FETCH_ASSOC);
$stmt->execute();
var_dump($stmt);
}
?>
@thewasta
Copy link
Author

select('table_name',['col_1','col_2'],['user'=>'username'])

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment