从MySQL导入的阵列工作,但阵列无法通过PHP作为一个数组? [英] An imported array from mysql works, but the array can't be used by php as an array?

查看:120
本文介绍了从MySQL导入的阵列工作,但阵列无法通过PHP作为一个数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在code以下不会因为该行 $ PARAMS =阵列($数据)的工作; 。它需要超过$数据以外的东西。或者,它需要的东西与在此之前线$数据的情况发生。

如果该行被写为$ PARAMS =阵列(A,B,C,D);那么它的伟大工程,但我的数组是在$数据变量,而不是写出来这样的。如果有一种方式来获得转换为被写入了这样的阵列,将工作过。

最终的结果应显示的数组的内容每一种可能组合(未排列)。如上面这表明ABC,BD,示例等。

  $ =数据的mysql_query(SELECT体重从my_table的WHERE SESSION_ID ='SESSION_ID()。'');$ PARAMS =阵列($的数据);$组合= getCombinations($ params)方法;
功能getCombinations($数组)
{
    $长度= sizeof的($数组);
    $ combocount = POW(2,$长度);
为($ i = 1; $ I< $ combocount; $ I ++)
    {$二进制= str_pad(decbin($ i)中,$长度,0,STR_PAD_LEFT);
        $组合='';
        为($ J = 0; $ J< $长度; $ J ++)
        {
            如果($二进制[$ J] ==1)
                。$组合= $阵列[$ J]。
        }
        $ combinationsarray [] = $组合;
        回声$组合。&放大器; LT; BR&放大器; GT;
    }
    返回$ combinationsarray;
}


解决方案

的mysql_query()只返回一个结果资源ID。检索数据,您必须使用获取命令之一,例如:

  $ PARAMS =阵列();
而($行= mysql_fetch_assoc($数据)){
    $ PARAMS [] = $行['重量'];
}

另外,您的查询可能是容易受到SQL注入。我不会含蓄地信任来自 SESSION_ID()的值。我不能完全肯定这一点,但它可能只是检索会话cookie的值。

最起码,消毒与值mysql_real_escape_string()。一个更强大的解决方案这将带给你code走出黑暗时代是使用PDO和参数绑定。

The code below won't work because of this line $params=array($data);. It needs something other than $data. Or it needs something to happen with $data prior to this line.

If the line is written as $params=array("A", "B", "C", "D"); then it works great, but my array is in the $data variable, not written out like that. If there is a way to get the array converted to being written out like that, that would work too.

The end result should show every possible combination (not permutation) of the contents of the array. Like in the example above it shows ABC, BD, etc.

$data = mysql_query('SELECT weight FROM my_table WHERE session_id = "' . session_id() . '"'); 

$params=array($data);

$combinations=getCombinations($params);
function getCombinations($array)
{
    $length=sizeof($array);
    $combocount=pow(2,$length);
for ($i=1; $i<$combocount; $i++)
    {

$binary = str_pad(decbin($i), $length, "0", STR_PAD_LEFT);
        $combination='';
        for($j=0;$j<$length;$j++)
        {
            if($binary[$j]=="1")
                $combination.=$array[$j];
        }
        $combinationsarray[]=$combination;
        echo $combination."&lt;br&gt;";
    }
    return $combinationsarray;
} 

解决方案

mysql_query() only returns a result resource ID. To retrieve data, you must use one of the "fetch" commands, for example

$params = array();
while ($row = mysql_fetch_assoc($data)) {
    $params[] = $row['weight'];
}

Also, your query is possibly vulnerable to SQL injection. I wouldn't implicitly trust the value from session_id(). I'm not entirely sure of this but it may simply retrieve the value from the session cookie.

At the very least, sanitise the value with mysql_real_escape_string(). A more robust solution which would bring your code out of the dark ages would be to use PDO and parameter binding.

这篇关于从MySQL导入的阵列工作,但阵列无法通过PHP作为一个数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆