简体   繁体   English

从php中的mysql同时获取数组中的列名和值

[英]get column name and values in array at same time from mysql in php

I am looking out for some kind of short code where i should not be repeating the column names in database and it should directly be inserted with respective column name index to array value .我正在寻找某种短代码,我不应该在其中重复数据库中的column names ,而应该直接将其与相应的column name索引一起插入到array value中。 Below i have uploaded the image for understanding.下面我上传了图片以供理解。

在此处输入图像描述

Use mysql_fetch_assoc() rather than mysql_fetch_row() and the array will be associative (using the column names as the key) rather than numeric.使用mysql_fetch_assoc()而不是mysql_fetch_row()并且数组将是关联的(使用列名作为键)而不是数字。

$query = mysql_query("SELECT * FROM users_table WHERE User_ID = 1");

while ($r1 = mysql_fetch_assoc($query))
{
  echo $r1['User_ID'] . ': ' . $r1['First_Name'] . ' ' . $r1['Last_Name'] . '<br />';
}

Check this:检查这个:

$query = mysql_query( "SELECT * FROM users_table WHERE user_id=1;" );
$row = mysql_fetch_array( $query );
print_r( $row );

more about mysql_fetch_array on php.net更多关于 php.net 上的mysql_fetch_array

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM