2017-04-17 89 views
0

如何顯示其各自數據的列名。在MySQL中顯示列名與數據

$sql="SELECT * FROM <Tablename> WHERE domain_name='google.com'"; 
$result=mysqli_query($conn,$sql); 
$row=mysqli_fetch_array($result,MYSQLI_NUM); 
echo "<table>"; 
for ($i=0; $i <sizeof($row) ; $i++) { 
    echo "<tr>"; 
    echo "<td>"; 
    echo mysql_field_name($row,$i); // its not working. 
    echo $row[$i]."<br/>"; 
    echo "</td>"; 
    echo "</tr>"; 
} 
echo "</table>"; 

請幫忙。

+2

你正在'mysql_'與'mysqli_'混合使用:http://php.net/manual/en/mysqli-result.fetch-field.php – Thamilan

+0

你使用的是mysql還是mysqli? –

+0

mysqli_field_name()不起作用。能否請你幫忙? 我是一種新的php – Siddhesh

回答

2

您可以使用fetch_field_direct

for ($i=0; $i <sizeof($row) ; $i++) { 
    ... 
    $finfo = $result->fetch_field_direct($i); 
    echo $finfo->name; 
    ... 
} 
+0

工作。非常感謝! :) – Siddhesh

0

試試這個代碼

if ($result->num_rows > 0) { 
    while($row = mysqli_fetch_array($result,MYSQLI_NUM)) { 
    echo "<tr>"; 
    echo "<td>"; 
    echo $row[0]."<br/>"; 
    echo "</td>"; 
    echo "</tr>"; 
    } 
    echo "</table>"; 
} else { 
    echo "0 results"; 
} 
$conn->close(); 
+0

感謝您的努力。以上代碼正在工作。檢查出。 – Siddhesh

0

使用mysqli_fetch_assoc獲取列名。

$sql='SELECT * FROM mytesttable WHERE username="as"'; 
$result=mysqli_query($conn,$sql); 
$row=mysqli_fetch_assoc($result); 

header('Content-type: application/json'); 
echo json_encode($row); 

你會得到output as json response。在未來這可能會幫助,如果你需要輸出json format。 謝謝,希望這有助於他人。