Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Old 11-15-2010, 07:16 AM Mysqli Bind Param
evans123's Avatar
Ultra Talker

Posts: 468
Trades: 0
I have a database class which contains a function called query, basically this function handles all the requests to the database etc.

When binding the parameters, i would like to know if it was succesful or not. For example, say i tried to bind_params on array('i', 'This is not an integer') then this shouldn't work as the value is not an integer. Any ideas on how i could implement such code into my function below.

PHP Code:
function query($sql$params$close){
           
$mysqli = new mysqli("host""user""pass""name");
          
           
$stmt $mysqli->prepare($sql) or die ("Failed to prepared the statement!");
          
           
call_user_func_array(array($stmt'bind_param'), $this->refValues($params));
           
           
$stmt->execute();
          
           if(
$close){
               
$result $mysqli->affected_rows;
           } else {
               
$meta $stmt->result_metadata();
           
               while ( 
$field $meta->fetch_field() ) {
                   
$parameters[] = &$row[$field->name];
               } 
       
            
call_user_func_array(array($stmt'bind_result'), $this->refValues($parameters));
              
            while ( 
$stmt->fetch() ) { 
               
$x = array(); 
               foreach( 
$row as $key => $val ) { 
                  
$x[$key] = $val
               } 
               
$results[] = $x
            }

            
$result $results;
           }
          
           
$stmt->close();
           
$mysqli->close();
          
           return  
$result;
   }
  
    function 
refValues($arr){
        if (
strnatcmp(phpversion(),'5.3') >= 0//Reference is required for PHP 5.3+
        
{
            
$refs = array();
            foreach(
$arr as $key => $value)
                
$refs[$key] = &$arr[$key];
            return 
$refs;
        }
        return 
$arr;
    } 
evans123 is offline
Reply With Quote
View Public Profile Visit evans123's homepage!
 
 
Register now for full access!
Old 11-15-2010, 08:15 AM Re: Mysqli Bind Param
evans123's Avatar
Ultra Talker

Posts: 468
Trades: 0
Ignore, webmaster-talk.com posted it twice.

Last edited by evans123; 11-15-2010 at 08:16 AM..
evans123 is offline
Reply With Quote
View Public Profile Visit evans123's homepage!
 
Old 11-15-2010, 08:16 AM Re: Mysqli Bind Param
evans123's Avatar
Ultra Talker

Posts: 468
Trades: 0
When i execute the following statement i shouldn't get any results but i do.

PHP Code:
    $id "1";
    
$file_name "home6.php";
$results $database->query("SELECT * FROM xxx WHERE id = ? && file_name = ?", array('ii'$id$file_name), false);
print_r($results); 
For starters the variable $file_name isnt a integer so the script should have shown an error. It always seems to only take the first part of the where clause, so when the file_name isnt correct it still shows a record.
evans123 is offline
Reply With Quote
View Public Profile Visit evans123's homepage!
 
Reply     « Reply to Mysqli Bind Param
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.27848 seconds with 12 queries