Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/255.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 获取_result()函数的替代方法?_Php_Mysql_Mysqli - Fatal编程技术网

Php 获取_result()函数的替代方法?

Php 获取_result()函数的替代方法?,php,mysql,mysqli,Php,Mysql,Mysqli,因此,我的主机没有为mysqli安装mysqlnd驱动程序,因此我无法使用get_result()函数(并且它不是安装mysqlnd驱动程序的选项)。以下查询的替代方法是什么,以便我不使用get\u result() 我知道我可以使用以下方法从数据库中获取单个结果: if ($stmt = $cxn->prepare("SELECT `count` FROM `numbers` WHERE `count_id` = ?")) { $stmt->bind_param('i',

因此,我的主机没有为mysqli安装mysqlnd驱动程序,因此我无法使用
get_result()
函数(并且它不是安装mysqlnd驱动程序的选项)。以下查询的替代方法是什么,以便我不使用
get\u result()

我知道我可以使用以下方法从数据库中获取单个结果:

if ($stmt = $cxn->prepare("SELECT `count` FROM `numbers` WHERE `count_id` = ?")) {
    $stmt->bind_param('i', $count_id);
    $stmt->execute();
    $stmt->bind_result($count);
    $stmt->fetch();
    $stmt->close();
}
但是,如果我必须从数据库中选择几个结果,循环每个结果,然后得到另一个结果呢

if ($stmt = $cxn->prepare("SELECT `count` FROM `numbers` WHERE `number` = ?")) {
    $stmt->bind_param('i', $number);
    $stmt->execute();
    $result = $stmt->get_result(); // uses get_result()
}

if ($result->num_rows) { // uses get_result()
    while ($row = $result->fetch_assoc()) { // uses get_result()
        if($stmt = $cxn->prepare("SELECT `value` FROM `values` WHERE `number` = ?")) {
            $stmt->bind_param('i', $row['count']);
            $stmt->execute();
            $stmt->bind_result($value);
            $stmt->fetch();
            $stmt->close();
        }
    }
}
我不能使用它,因为有些语句需要
get\u result()
,我无法使用它

我的选择是什么?

试试这个

if ($stmt = $cxn->prepare("SELECT `count` FROM `numbers` WHERE `count_id` = ?")) 
{

    $stmt->bind_param('i', $count_id);
    $stmt->execute();
    $result = $stmt->fetchAll();

        if ( count($result) ) 
        {

            foreach($result as $row) 
            {

                print_r($row);

            }
        } 
        else 
        {

            echo "Nothing Returned.";

        }
    }

我不完全确定为什么这个函数在一些PHP安装中不存在,但我在一个项目中发现了它,所以我已经实现了这个几乎可以替代的功能。 此函数将“mysqli_stmt”对象作为参数,如果该对象存在,则将调用该对象上的方法。如果没有,它将使用“mysqli_result”类的这个部分实现(我没有使用的一些方法缺失)

“convert_array”函数处理与“$mode”参数关联的所有数组合并功能

function convert_array($assoc, $mode)
{
    $outarray = [];

    switch ($mode)
    {
        case MYSQLI_ASSOC:
            foreach($assoc as $key => $value)
            {       
                if (is_array($value))           
                    $outarray[$key] = convert_array($value, $mode);                                         
                else            
                    $outarray[$key] = $value;           
            }   
            return $outarray;
        case MYSQLI_NUM:
            $i = 0;
            foreach($assoc as $key => $value)
            {       
                if (is_array($value))           
                    $outarray[$i] = convert_array($value, $mode);                                           
                else            
                    $outarray[$i] = $value;         
                $i++;
            }   
            return $outarray;           
        case MYSQLI_BOTH:
            $i = 0;         
            foreach($assoc as $key => $value)
            {       
                if (is_array($value))                           
                    $outarray[$key] = $outarray[$i] = convert_array($value, $mode);                                         

                else            
                    $outarray[$key] = $outarray[$i] = $value;           
                $i++;
            }   
            return $outarray;
    }

    return $outarray;


}

//drop in replacement for the sometimes missing (depending on version) 'stmt->get_result'
class proxy_result
{
    public $current_field = 0;
    public $field_count = 0;
    public $lengths = 0;
    public $num_rows = 0;
    public $current_row = 0;
    public $type = 0;

    public $results = [];  

    function fetch_all ($resulttype = MYSQLI_NUM)
    {
        return convert_array($this->results, $resulttype);
    }


    //fetch row as associative array
    function fetch_assoc()
    {
        if ($this->current_row < $this->num_rows)
            return $this->results[$this->current_row++];
        else
            return null;        
    }

    function fetch_array($resulttype = MYSQLI_BOTH)
    {       
        return convert_array($this->fetch_assoc(), $resulttype);
    }  
};


function get_result( $stmt ) 
{
    if (method_exists ($stmt ,'get_result'))
    {
        return $stmt->get_result();
    }
    else
    {
        $RESULT = array();
        $stmt->store_result();
        for ( $i = 0; $i < $stmt->num_rows; $i++ ) 
        {
            $Metadata = $stmt->result_metadata();
            $PARAMS = array();
            while ( $Field = $Metadata->fetch_field() ) 
            {
                $PARAMS[] = &$RESULT[ $i ][ $Field->name ];
            }
            call_user_func_array( array( $stmt, 'bind_result' ), $PARAMS );
            $stmt->fetch();

            $result = new proxy_result();
            $result->num_rows = count($RESULT);
            $result->field_count = count($RESULT[0]);
            $result->results = $RESULT;

            //$RESULT->num_rows = count($RESULT);  
        }

        return $result;
    }   
}
函数转换\u数组($assoc$mode)
{
$outarray=[];
交换机($模式)
{
案件MYSQLI_助理:
foreach($assoc as$key=>$value)
{       
if(是_数组($value))
$outarray[$key]=转换数组($value$mode);
其他的
$outarray[$key]=$value;
}   
返回$outarray;
案例MYSQLI_NUM:
$i=0;
foreach($assoc as$key=>$value)
{       
if(是_数组($value))
$outarray[$i]=转换数组($value$mode);
其他的
$outarray[$i]=$value;
$i++;
}   
返回$outarray;
案例MYSQLI_两个:
$i=0;
foreach($assoc as$key=>$value)
{       
if(是_数组($value))
$outarray[$key]=$outarray[$i]=转换数组($value,$mode);
其他的
$outarray[$key]=$outarray[$i]=$value;
$i++;
}   
返回$outarray;
}
返回$outarray;
}
//替换有时丢失的(取决于版本)“stmt->get_result”
类代理结果
{
公共$current\u字段=0;
公共$field\u count=0;
公共资源长度=0;
公共$num_行=0;
公共$current\u行=0;
公共$type=0;
公共$results=[];
函数fetch_all($resulttype=MYSQLI_NUM)
{
返回convert\u数组($this->results,$resulttype);
}
//将行提取为关联数组
函数fetch_assoc()
{
如果($this->current_row<$this->num_rows)
返回$this->results[$this->current_row++];
其他的
返回null;
}
函数fetch_数组($resulttype=MYSQLI_)
{       
返回convert_数组($this->fetch_assoc(),$resulttype);
}  
};
函数获取结果($stmt)
{
如果(方法_存在($stmt,'get_result'))
{
返回$stmt->get_result();
}
其他的
{
$RESULT=array();
$stmt->store_result();
对于($i=0;$i<$stmt->num_行;$i++)
{
$Metadata=$stmt->result_Metadata();
$PARAMS=array();
而($Field=$Metadata->fetch_Field())
{
$PARAMS[]=&$RESULT[$i][$Field->name];
}
调用_user_func_数组(数组($stmt,'bind_result'),$PARAMS);
$stmt->fetch();
$result=新代理_result();
$result->num_rows=count($result);
$result->field_count=count($result[0]);
$result->results=$result;
//$RESULT->num_rows=count($RESULT);
}
返回$result;
}   
}

这不是一个富有成效的答案,但老实说,我会找到另一个主机。我没有运气找到另一个可以按月计费、价格便宜且有cPanel的主机。