从MySQL PHP数组 [英] php array from mysql
问题描述
我使用PHP来从MySQL获得的值。我的表有以下形式:
I'm using PHP to get a value from MySQL. My table has this form:
COL1 = MAIL mail@mail.com
COL2 = NAME myname
和我从数据库中获取用户的行。
and I'm getting from the database the row of the user.
$query = mysqli_query($conn, "select * from user where mail = 'telmo@mail.com'");
$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] => mail@mail.com [MAIL] => mail@mail.com [1] => myname [NAME] => myname )
我学习PHP的,我想知道如果这应该是这个样子。因为我想要的东西更像是:
I'm learning PHP and I'm wondering if is this supposed to be like this. Because I would want something more like:
MAIL => mail@mail.com
NAME => myname
也许我没有得到这个语法非常好,但是这就是为什么我寻求帮助。
在此先感谢
Maybe I'm not getting this syntax very well, but this is why I'm looking for help. Thanks in advance
感谢您提前
推荐答案
无论您的查询后指定MYSQLI_ASSOC,或使用mysqli_fetch_assoc。从手动:
Either specify MYSQLI_ASSOC after your query, or use mysqli_fetch_assoc. from the manual:
与resultType可选参数是表示什么类型的常数
阵列应该从当前行数据来产生。可能
此参数值是常量MYSQLI_ASSOC,MYSQLI_NUM,
或MYSQLI_BOTH。
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.
通过使用MYSQLI_ASSOC不变这一功能的行为
相同的mysqli_fetch_assoc(),而MYSQLI_NUM将表现
相同的mysqli_fetch_row()函数。最后一个选项
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.
手册: http://us2.php.net/mysqli_fetch_array
这篇关于从MySQL PHP数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!