Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
300 views
in Technique[技术] by (71.8m points)

Mysql extracting array in Php

What is the problem with my mysql array extraction below:

        $mysql_array = mysql_query("SELECT * FROM table1 WHERE uid1='$uid1'"); 
        $array = array();
        while($row = mysql_fetch_assoc($mysql_array)){
            $array[] = $row;
        }

        $array = array_unique($array);
        $array = array_reverse($array);
        $emails = array();
        $numbers = array();

        foreach($array as $row){
            $uid2 = $row['uid2'];
            $number = number($uid2);
            if(strlen($number) > 9){
                $numbers[] = array('uid2' => $uid2, 'number' => $number);
            }
            else{
                $email = email($uid2);
                $emails[] = array('uid2' => $uid2, 'email' => $email);
            }
        }
        $numbers = array_unique($numbers);
        $emails = array_unique($emails);            
        var_dump($numbers);
        var_dump($emails);

There must be something I need to do to convert the "Resource" from mysql into an array. There must be a problem with the above code. This is what I am getting on the var_dumps: array(0) { } and array(1) { [0]=> array(2) { ["uid2"]=> NULL ["email"]=> NULL } }

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Look at this foreach:

        foreach($array as $uid2){
            $uid2 = $array['uid2'];
            $number = number($yfbid);
            if(strlen($number) > 9){
                $numbers[] = array('uid2' => $uid2, 'number' => $number);
            }
            else{
                $email = email($uid2);
                $yfbid[] = array('uid2' => $uid2, 'email' => $email);
            }
        }

You iterate all positions of array and call them $uid2. Then, in the first line you do $uid2 = $array['uid2'];. You loose your array position.

Maybe you wanted something like: $var = $uid2['uid2'];


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...