0

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 protected]
2 | Test | [email protected]
2 | Hi | [email protected]
1 | Foo | [email protected]
1 | Bar | [email protected]

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.

3 Answers 3

2

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)

Sign up to request clarification or add additional context in comments.

Comments

0
$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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.