开发者

Array from mysql_fetch_array with a query selecting everything isn't holding all the values

So, I have a database (With a table called users) that let's users connect accounts, and it goes by their usernumber to find their accounts, let's say (With values):

Usernumber|Username|Email
-------------------------
1 | Justin | email@email.com
2 | Test | test@test.com
2 | Hi | gah@goo.com
1 | Foo | hey@hey.com
1 | Bar | Du@de.com

Now, in PHP, I do:

$stuff = mysql_fetch_array(mysql_query("SELECT * FROM users WHERE usernumber=1"));

Now, I 开发者_如何转开发do count($stuff); and it returns 14, all the time, whether I do it with usernumber one or two... I can't figure out the problem, but I was wondering if someone else sees it, or knows something that I don't about how certain files are configured (Limits or something?). I'm using XAMPP to test everything right now.

ALSO, when I do the $stuff in PHPMYADMIN as a SQL in the table, it does return all the values it should.

So question is: What am I missing and how do I get it return all the values and not just 2 rows? (The table has 7 columns).

Thanks, Justin W.


By default, mysql_fetch_array will return a combined indexed array of columns (0 - 6) and associative array (column_name => value)

That is why you have 14 values for that row (7 that are numerically indexed, 7 that are associatively indexed).

Also, you only get one row per function call. 14 is the number of columns (7 * 2), not the number of rows.

You need to start holding your MySQL result resource in its own variable:

$result = mysql_query("SELECT * FROM users WHERE usernumber=1");

That way you can call mysql_fetch_array multiple times on that resultset. Also, you can check the number of rows by calling mysql_num_rows($result)


$result = mysql_query("SELECT * FROM users WHERE usernumber = 1");
while($row = mysql_fetch_assoc($result)){
    print_r($row);
}

Try this to see if that result is what you want.


It is possible that you get that value since when you do mysql_fetch_array(), you get results in your array like this:

$stuff[0] => "1"; 
$stuff['usernumber'] => "1"; 
$stuff[1] => "justin";
$stuff['username'] => "justin";

,

and so on, do print_r(), or var_dump() and see the actual values inside the $stuff array.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜