mysql_field_name到新的mysqli

时间:2013-01-31 15:47:47

标签: php mysqli

我有办法获取表格列的名称。它工作正常,但现在我想更新到新的mysqli? (我尝试了mysqli_fetch_field,但我不知道如何应用于这种情况,我不确定它是否是wright选项)

如何用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;

5 个答案:

答案 0 :(得分:30)

这是实现此缺失功能的方法:

function mysqli_field_name($result, $field_offset)
{
    $properties = mysqli_fetch_field_direct($result, $field_offset);
    return is_object($properties) ? $properties->name : null;
}

答案 1 :(得分:16)

我不确定是否有更好的方法可以做到这一点,但我检查过这只能得到列的名称并且是新的mysqli:

$result = mysqli_query($con, 'SELECT * FROM myTable');
while ($property = mysqli_fetch_field($result)) {
    echo $property->name;
}

答案 2 :(得分:5)

您可以将mysql_field_name功能替换为mysqli_fetch_field_direct并使用它,如下所示:

$colObj = mysqli_fetch_field_direct($result,$i);                            
$col = $colObj->name;
echo "<br/>Coluna: ".$col;

答案 3 :(得分:2)

这是打印每个字段的名称,表格和最大长度的另一种简单方法

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />

<div class="container">
  <div class="row">

    <div class="col-xs-12 col-sm-4">
      <label><strong>UK Postcode</strong></label>
      <div class="form-group">
        <input type="text" class="form-control">
      </div>
    </div>

    <div class="col-xs-12 col-sm-1">
      <div class="invisible hidden-xs">
        &nbsp;
        <!-- Just to give similar white space above OR text but as well hide this gap in mobile devices. -->
      </div>
      <div class="form-group">
        <strong>OR</strong>
      </div>
    </div>

    <div class="col-xs-12 col-sm-4">
      <label><strong>Other Field</strong></label>
      <div class="form-group">
        <input type="text" class="form-control">
      </div>
    </div>
  </div>
</div>

答案 4 :(得分:1)

$sql = "SELECT * from myTable";
$res = mysqli_query($con,$sql);
$row = mysqli_fetch_assoc($res);
$fields = array_keys($row);
var_dump($fields);