Resident Kender
mysqli bind_param error i believe, in SELECT FROM
using this function it should take the query and check results against the type (integer, string, blob, decimal), if the result is correct type return it, if not then return error or nothing
it executes correctly if type is correct, but if type is wrong it returns all results instead... not sure where it went wrong
any help is always appreciated!
thanks
PHP Code:
<?php
$sql = "SELECT * FROM user WHERE last_name=? && first_name=?" ;
$typeDef = array( "s" , "s" );
$data = array( "jones" , "jim" );
$query = sql_query ( $sql , $typeDef , $data );
echo '<pre>' ; print_r ( $query ); echo '</pre>' ;
function sql_query ( $sql , $typeDef , $data ) {
$mysqli = mysqli_con ();
$type = '' ; $multiQuery = '' ;
foreach( $typeDef AS $val ) { $type .= $val ; }
if( $stmt = $mysqli -> prepare ( $sql )) {
if( count ( $data ) == count ( $data , 1 )) {
$data = array( $data );
$multiQuery = FALSE ;
} else {
$multiQuery = TRUE ;
}
if( $type ) {
$bindParams = array();
$bindParamsReferences = array();
$bindParams = array_pad ( $bindParams ,( count ( $data , 1 )- count ( $data ))/ count ( $data ), "" );
foreach( $bindParams as $key => $value ) {
$bindParamsReferences [ $key ] = & $bindParams [ $key ];
}
array_unshift ( $bindParamsReferences , $type );
$bindParamsMethod = new ReflectionMethod ( 'mysqli_stmt' , 'bind_param' );
$bindParamsMethod -> invokeArgs ( $stmt , $bindParamsReferences );
}
$result = array();
foreach( $data as $queryKey => $query ) {
foreach( $bindParams as $paramKey => $value ) {
$bindParams [ $paramKey ] = $query [ $paramKey ];
}
$queryResult = array();
if( mysqli_stmt_execute ( $stmt )) {
$resultMetaData = mysqli_stmt_result_metadata ( $stmt );
if( $resultMetaData ) {
$stmtRow = array();
$rowReferences = array();
while ( $field = mysqli_fetch_field ( $resultMetaData )) {
$rowReferences [] = & $stmtRow [ $field -> name ];
}
mysqli_free_result ( $resultMetaData );
$bindResultMethod = new ReflectionMethod ( 'mysqli_stmt' , 'bind_result' );
$bindResultMethod -> invokeArgs ( $stmt , $rowReferences );
while( mysqli_stmt_fetch ( $stmt )) {
$row = array();
foreach( $stmtRow as $key => $value ) {
$row [ $key ] = $value ;
}
$queryResult [] = $row ;
}
mysqli_stmt_free_result ( $stmt );
} else {
$queryResult [] = mysqli_stmt_affected_rows ( $stmt );
}
} else {
$queryResult [] = FALSE ;
}
$result [ $queryKey ] = $queryResult ;
}
mysqli_stmt_close ( $stmt );
} else {
$result = FALSE ;
}
if( $multiQuery ) {
return $result ;
} else {
return $result [ 0 ];
}
}
?>
Resident Kender
I guess its bind_result error?
still cant find where the error is though
Resident Kender
I think the error is in this segment of code, but cant find a resolution
PHP Code:
$bindResultMethod = new ReflectionMethod ( 'mysqli_stmt' , 'bind_result' ); $bindResultMethod -> invokeArgs ( $stmt , $rowReferences ); while( mysqli_stmt_fetch ( $stmt )) { $row = array(); foreach( $stmtRow as $key => $value ) { $row [ $key ] = $value ; } $queryResult [] = $row ; }
Resident Kender
Nobody is able to shed any light on this?
Resident Kender
Still stuck, but after looking deeper i think the problem is not where i said before but it is here
PHP Code:
if( $type ) { $bindParams = array(); $bindParamsReferences = array(); $bindParams = array_pad ( $bindParams ,( count ( $where_vals , 1 )- count ( $where_vals ))/ count ( $where_vals ), "" ); foreach( $bindParams as $key => $value ) { $bindParamsReferences [ $key ] = & $bindParams [ $key ]; } array_unshift ( $bindParamsReferences , $type ); $bindParamsMethod = new ReflectionMethod ( 'mysqli_stmt' , 'bind_param' ); $bindParamsMethod -> invokeArgs ( $stmt , $bindParamsReferences ); }
Thread Information
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)
Tags for this Thread
Posting Permissions
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
Forum Rules
Bookmarks