[ create a new paste ] login | about

Link: http://codepad.org/CulVZwN0    [ raw code | fork ]

PHP, pasted on May 3:
<?php
/*
		WARNING: Nothing is guaranteed to work.
		
*/

function fSQL_SELECT($D,$T,$FV){	//	( Database, Table, Fields & Values Array ( 'id' => 'value' ) )

if( !is_array( $FV ) ){ return false; }				//	<-	Key-Value Array. Keys are Field Names, Values are values

$a = $D -> prepare( 'SHOW TABLES' );
$a -> execute();
$A = $a -> fetchAll( PDO::FETCH_COLUMN );			//	<-	Gets all Tables of your Database


if( !in_array( $T, $A ) ){ return false; }			//	<-	Makes sure your Table exists in your Database


$a = $D -> prepare( "DESCRIBE $T" );
$a -> execute();
$A = $a -> fetchAll( PDO::FETCH_COLUMN );			//	<-	Gets all Fields of your Table


$i = 0;
$S = '';
$R = array();
foreach( $FV as $F => $V ){
	if( !in_array( $F, $A ) ){ return false;  }		//	<-	Makes sure all Fields exists in your Table
	$S .= " `$F` = ? ";								//	<-	Sets up the String we will be using
	$R[ $i ] = $V;									//	<-	Sets up the Array to execute
	$i++;											//	<-	Used for Array key values and adding a comma in String
	if( $i < count( $FV ) ){ $S .= ' AND '; }			//	<-	Adds a comma to the String if needed
}

//die( "SELECT * FROM `$T` WHERE $S");
$a = $D -> prepare( "SELECT * FROM `$T` WHERE $S" );//<-Lets get this over with

$i = 0;
while( $i < count( $R ) ){
	$a -> bindValue( $i+1, $R[$i], PDO::PARAM_STR );//	<- Use a bindValue thing - i think its supposed to be safer
	$i++;
}
$a -> execute();									//	<-	Execute

$c = array();
while( $b = $a -> fetch( PDO::FETCH_ASSOC ) ){		//	<-	probably can be shorter, but creates array thing
	$i = 0;
	while( $i < count( $A ) ){
		$c [ $A[$i] ] = $b[ $A[$i] ];
		$i++;
	}
}

return $c;											//	<-	return that complete array package

}

?>



Example:

<?php
//$User = fSQL_SELECT( $DataB, 'account', array( 'email' => $_COOKIE['email'], 'password' => $_COOKIE['password'] ) );

//echo $User['username'];


?>


Create a new paste based on this one


Comments: