www.webdeveloper.com
Results 1 to 9 of 9

Thread: Failed to prepared the statement!

Threaded View

  1. #1
    Join Date
    Dec 2011
    Posts
    20

    Failed to prepared the statement! MySQL

    So I have a problem with prepare statement, at all everything works, but it won't work if I bind $table, any suggestions?
    execSQL("SELECT id FROM ? WHERE id=?", array('si', $table, $u_id), style::Q_NUM_ROWS)
    Code:
    const Q_GET_VALUE = 0;
    const Q_NUM_ROWS = 1;
    const Q_AFFECTED_ROWS = 2;
    
    function user_exists($u_id, $table)
    {
    	if ( $this->execSQL("SELECT id FROM ? WHERE id=?", array('si', $table, $u_id), style::Q_NUM_ROWS) != 0 )
    		return true;
    		
    	return false;
    }
    	function execSQL($sql, $params, $exec_type)
    	{
    		$stmt = $this->db_connect->prepare($sql) or die ("Failed to prepared the statement!");
            
    		if ( !empty($params) )
    			call_user_func_array(array($stmt, 'bind_param'), $this->refValues($params));
        
    		$stmt->execute();
                
    		if ( $exec_type == self::Q_GET_VALUE ) 
    		{
    			$parameters = array();
    		
    			$meta = $stmt->result_metadata();
                 
    			while ( $field = $meta->fetch_field() ) {
    				$parameters[] = &$row[$field->name];
    			}  
             
    			call_user_func_array(array($stmt, 'bind_result'), $this->refValues($parameters));
                    
    			$stmt->fetch();
    		
    			$result = $row;
    		}
    		else if( $exec_type == self::Q_NUM_ROWS)
    		{
    			$stmt->store_result();
    			$result = $stmt->num_rows;
    		} 
    		else
    		{
    			$result = $stmt->affected_rows;
    		}
                
    		$stmt->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;
    	}
    Last edited by xbatista; 10-25-2012 at 11:58 AM.

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
  •  
HTML5 Development Center



Recent Articles