Skip to content

Instantly share code, notes, and snippets.

@pingpoli
Created July 7, 2020 16:00
Show Gist options
  • Save pingpoli/09be50fbf5037aa1707d181b85df1394 to your computer and use it in GitHub Desktop.
Save pingpoli/09be50fbf5037aa1707d181b85df1394 to your computer and use it in GitHub Desktop.
<?php
function lmysql_execute( $mysql , $sql , $types , ...$params )
{
if ( !( $stmt = $mysql->prepare( $sql ) ) )
{
error_log( "LMYSQL > prepare , errno: ".$mysql->errno.", error: ".$mysql->error );
return false;
}
if ( strlen( $types ) > 0 )
{
// mysqli_stmt->bind_param requires references, therefore the array passed to call_user_func_array also needs to contain references
$bindParamArray = array();
$bindParamArray[] = &$types;
for ( $i = 0 ; $i < strlen( $types ) ; ++$i ) $bindParamArray[] = &$params[$i];
if ( !( call_user_func_array( array( $stmt , 'bind_param' ) , $bindParamArray ) ) )
{
$stmt->close();
error_log( "LMYSQL > call_user_func_array , errno: ".$mysql->errno.", error: ".$mysql->error );
return false;
}
}
if ( !( $stmt->execute() ) )
{
$stmt->close();
error_log( "LMYSQL > execute , errno: ".$mysql->errno.", error: ".$mysql->error );
return false;
}
$stmt->close();
return true;
}
function lmysql_select( $mysql , $sql , $types , ...$params )
{
if ( !( $stmt = $mysql->prepare( $sql ) ) )
{
error_log( "LMYSQL > errno: ".$mysql->errno.", error: ".$mysql->error );
return false;
}
if ( strlen( $types ) > 0 )
{
// mysqli_stmt->bind_param requires references, therefore the array passed to call_user_func_array also needs to contain references
$bindParamArray = array();
$bindParamArray[] = &$types;
for ( $i = 0 ; $i < strlen( $types ) ; ++$i ) $bindParamArray[] = &$params[$i];
if ( !( call_user_func_array( array( $stmt , 'bind_param' ) , $bindParamArray ) ) )
{
$stmt->close();
error_log( "LMYSQL > errno: ".$mysql->errno.", error: ".$mysql->error );
return false;
}
}
if ( !( $stmt->execute() ) )
{
$stmt->close();
error_log( "LMYSQL > errno: ".$mysql->errno.", error: ".$mysql->error );
return false;
}
if ( !( $result = $stmt->get_result() ) )
{
$stmt->close();
error_log( "LMYSQL > errno: ".$mysql->errno.", error: ".$mysql->error );
return false;
}
$stmt->close();
return $result;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment