Last active
December 10, 2018 00:27
-
-
Save thewasta/f0f3fc58b82ecd2c0225dc9567a85270 to your computer and use it in GitHub Desktop.
Select from DB with a condicional WHERE
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
select('table_name',['col_1','col_2'],['user'=>'username'])