开发者

An imported array from mysql works, but the array can't be used by php as an array?

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.


$data is not an array. Assuming mysql_query() did not return an error or an empty result (both of which you should check for, by the way--lookup documentation for mysql_error() and mysql_num_rows() perhaps, maybe some others), $data is a resource.

So you want $params=mysql_fetch_array($data) to make it an array. (That assumes that there is only one result. If it might return more than one row, you'll probably want to wrap it in a loop. If you are 100% certain that session_id is unique , then you can get away without the loop, I suppose. You can also get away without the loop if you only care about the first result in a multi-row result, although I'd throw in a LIMIT 1 in your query in that case to improve performance.)

There are lots of options (do you want a numerically indexed array, or one where they keys are the names of the columns, etc.) so read up at http://www.php.net/manual/en/function.mysql-fetch-array.php.


Ok there are alot of fundamental problems with your script. I personally recommend to first read this article and then about the actual function called mysql_fetch_array().

Simply put, what you are receiving from mysql is resource (corrent me if i'm wrong!) and you have to fetch array on that.

$params = mysql_fetch_array($data);

PS: This makes no sense: $params=array($data);

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜