Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm using PHP to get a value from MySQL. My table has this form:

COL1 = MAIL [email protected]
COL2 = NAME myname

and I'm getting from the database the row of the user.

$query = mysqli_query($conn, "select * from user where mail = '[email protected]'");
$row = mysqli_fetch_array($query);

print_r($row);

But my output is very strange, it gives my like a bidimensional array like this:

Array ( [0] => [email protected] [MAIL] => [email protected] [1] => myname [NAME] => myname )

I'm learning PHP and I'm wondering if is this supposed to be like this. Because I would want something more like:

MAIL => [email protected]
NAME => myname

Maybe I'm not getting this syntax very well, but this is why I'm looking for help. Thanks in advance

Thank you in Advance

share|improve this question
add comment

2 Answers

From the manual

mysqli_fetch_array() is an extended version of the mysqli_fetch_row() function. In addition to storing the data in the numeric indices of the result array, the mysqli_fetch_array() function can also store the data in associative indices, using the field names of the result set as keys.

What you you're really looking for is mysqli_fetch_assoc()

Returns an associative array that corresponds to the fetched row or NULL if there are no more rows.

share|improve this answer
 
Thank you very much –  Telmo Vaz Nov 15 '13 at 0:52
add comment

Either specify MYSQLI_ASSOC after your query, or use mysqli_fetch_assoc. from the manual:

resulttype This optional parameter is a constant indicating what type of array should be produced from the current row data. The possible values for this parameter are the constants MYSQLI_ASSOC, MYSQLI_NUM, or MYSQLI_BOTH.

By using the MYSQLI_ASSOC constant this function will behave identically to the mysqli_fetch_assoc(), while MYSQLI_NUM will behave identically to the mysqli_fetch_row() function. The final option MYSQLI_BOTH will create a single array with the attributes of both.

Manual: http://us2.php.net/mysqli_fetch_array

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.