php mysql_field_name 到新的 mysqli
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14629636/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
mysql_field_name to the new mysqli
提问by Nrc
I have a way to get the name of the columns of a table. It works fine but now I want to update to the new mysqli ? (I tried the mysqli_fetch_field but I don't know how to apply to this case and I am not sure if it is the wright option)
我有一种方法可以获取表列的名称。它工作正常,但现在我想更新到新的 mysqli ?(我尝试了 mysqli_fetch_field 但我不知道如何应用于这种情况,我不确定它是否是 wright 选项)
How to do the same with mysqli ? :
如何对 mysqli 做同样的事情?:
$sql = "SELECT * from myTable";
$result = mysql_query($sql,$con);
$id = mysql_field_name($result, 0);
$a = mysql_field_name($result, 1);
echo $id;
echo $a;
采纳答案by Nrc
I'm not sure if there is a better way to do that, but I checked that this works to get just the name of the columns and is the new mysqli :
我不确定是否有更好的方法来做到这一点,但我检查了这是否可以获取列的名称并且是新的 mysqli :
$result = mysqli_query($con, 'SELECT * FROM myTable');
while ($property = mysqli_fetch_field($result)) {
echo $property->name;
}
回答by José Carlos PHP
This is the way to implement this missing function:
这是实现此缺失功能的方法:
function mysqli_field_name($result, $field_offset)
{
$properties = mysqli_fetch_field_direct($result, $field_offset);
return is_object($properties) ? $properties->name : null;
}
回答by user3187518
You can replace the function mysql_field_nameto mysqli_fetch_field_directand use it like the following:
您可以将函数替换mysql_field_name为mysqli_fetch_field_direct并使用它,如下所示:
$colObj = mysqli_fetch_field_direct($result,$i);
$col = $colObj->name;
echo "<br/>Coluna: ".$col;
回答by A.A Noman
This is another easy way to print each field's name, table, and max length
这是打印每个字段的名称、表和最大长度的另一种简单方法
$sql="SELECT Lastname,Age FROM Persons ORDER BY Lastname";
if ($result=mysqli_query($con,$sql))
{
// Get field information for all fields
while ($fieldinfo=mysqli_fetch_field($result))
{
printf("Name: %s\n",$fieldinfo->name);
printf("Table: %s\n",$fieldinfo->table);
printf("max. Len: %d\n",$fieldinfo->max_length);
}
// Free result set
mysqli_free_result($result);
}
回答by Your Common Sense
$sql = "SELECT * from myTable";
$res = mysqli_query($con,$sql);
$row = mysqli_fetch_assoc($res);
$fields = array_keys($row);
var_dump($fields);

