mysql_field_name to the new mysqli

后端 未结 5 1464
生来不讨喜
生来不讨喜 2021-01-01 13:01

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 a

相关标签:
5条回答
  • 2021-01-01 13:32

    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 :

    $result = mysqli_query($con, 'SELECT * FROM myTable');
    while ($property = mysqli_fetch_field($result)) {
        echo $property->name;
    }
    
    0 讨论(0)
  • 2021-01-01 13:34

    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);
    }
    
    0 讨论(0)
  • 2021-01-01 13:43

    You can replace the function mysql_field_name to mysqli_fetch_field_directand use it like the following:

    $colObj = mysqli_fetch_field_direct($result,$i);                            
    $col = $colObj->name;
    echo "<br/>Coluna: ".$col;
    
    0 讨论(0)
  • 2021-01-01 13:51
    $sql = "SELECT * from myTable";
    $res = mysqli_query($con,$sql);
    $row = mysqli_fetch_assoc($res);
    $fields = array_keys($row);
    var_dump($fields);
    
    0 讨论(0)
  • 2021-01-01 13:57

    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;
    }
    
    0 讨论(0)
提交回复
热议问题