0

How to display column name for their respective data.

$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>";

Please help.

4
  • 2
    You are mixing mysql_ with mysqli_ : php.net/manual/en/mysqli-result.fetch-field.php Commented Apr 17, 2017 at 8:43
  • are you use mysql or mysqli ?? Commented Apr 17, 2017 at 8:45
  • mysqli_field_name() doesn't work. Could you please help? I'm kind of new to php Commented Apr 17, 2017 at 8:46
  • @EhsanIlahi I want to use mysqli Commented Apr 17, 2017 at 8:47

3 Answers 3

2

You can use fetch_field_direct:

for ($i=0; $i <sizeof($row) ; $i++) { 
   ...
   $finfo = $result->fetch_field_direct($i);
   echo $finfo->name;
   ...
}
Sign up to request clarification or add additional context in comments.

Comments

0

Try this code

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();

1 Comment

Thanks for your efforts. Above code is working. check that out.
0

Use mysqli_fetch_assoc to fetch the column names.

$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);

You will get output as json response.In future this may help,if you need to output in json format. Thanks, Hope this helps for others.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.