Why the first character of a foreach output array element

Source: Internet
Author: User
$sql =mysql_query ("Select B.userid,b.username,b.card_no,b.buytime,b.commission1,m.level from Buybak B left Joinmember M on B.userid=m.userid where m.agentid= ' ". $user [UserID]." ' and b.status1>=0 ");
$array = @mysql_fetch_array ($level 1_orders);
foreach ($array as $rows) {
echo $rows. '
';//loop output two arrays of information
echo $rows [' userid ']. '
';//This only outputs the first character of each element, if the value of username is test, then the T is output. What's going on?
}


With the while to output normally, I want to know how to use the foreach output


Reply to discussion (solution)

$array = @mysql_fetch_array ($level 1_orders);
Print_r ($array); Look what it is.

$array = @mysql_fetch_array ($level 1_orders);
Print_r ($array); Look what it is.



Print_r ($array) only outputs an array, while the two-dimensional array is looped, and two is the right one. foreach has no way of printing subscript.
Array
(
[0] = 3
[UserID] = 3
[1] = Test3
[Username] = Test3
[2] = VIP
[Card_no] = VIP
[3] = 2016-02-16 14:53:33
[Buytime] = 2016-02-16 14:53:33
[4] + = {"1": 5, "2": 8, "3": 10, "4": 12}
[Commission1] = {"1": 5, "2": 8, "3": 10, "4": 12}
[5] = 1
[Level] = 1
)

What is the while loop, print out the output to see

Second, the foreach traversal above you is traversing a two-dimensional array, but your $array is obviously a one-dimensional array.

$array = @mysql_fetch_array ($level 1_orders);
Print_r ($array); Look what it is.



This is the while loop, and this is right.
Array
(
[0] = = Array
(
[0] = 2
[id] = 2
[1] = 3
[UserID] = 3
[2] = Test3
[Username] = Test3
[3] = VIP
[Card_no] = VIP
[4] = 2016-02-16 14:53:33
[Buytime] = 2016-02-16 14:53:33
[5] + = {"1": 5, "2": 8, "3": 10, "4": 12}
[Commission1] = {"1": 5, "2": 8, "3": 10, "4": 12}
[6] = 1
[Level] = 1
)

[1] = = Array
(
[0] = 10
[id] = 10
[1] = 8
[UserID] = 8
[2] = Test8
[Username] = Test8
[3] = VIP
[Card_no] = VIP
[4] = 2016-02-16 14:59:13
[Buytime] = 2016-02-16 14:59:13
[5] + = {"1": 5, "2": 8, "3": 10, "4": 12}
[Commission1] = {"1": 5, "2": 8, "3": 10, "4": 12}
[6] = 1
[Level] = 1
)

)

Mysql_fetch_array ($level 1_orders) returns a one-dimensional array
So you need to use the while loop to execute mysql_fetch_array to read all the results

Traversing a one-dimensional array requires only one loop, and you're not going to have to use two of them naturally.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.