0

I am sending a sql query to fetch a data from database but the output is showing the column name as it's value. Following are the codes and output respectively:

<?php
include_once("connect_db.php");
$query1 = "SELECT 'first_name' FROM  user_details WHERE email='saptakds@gmail.com'";

    $result1 = mysqli_query($conn,$query1);
    $myArray = array();

    while($row = $result1->fetch_array(MYSQLI_ASSOC)) {

        $myArray[] = $row;
    }



    echo '{"maal":';
    echo json_encode($myArray, JSON_UNESCAPED_SLASHES);
    echo ',"message" : "success"}';
   ?>

Output:

{"maal":[{"first_name":"first_name"}],"message" : "success"}

The desired output should have been the following:

{"maal":[{"first_name":"Saptak"}],"message" : "success"}
Saptak Das
  • 33
  • 8

1 Answers1

2

You should remove the ' on SELECT:

SELECT first_name 
FROM user_details WHERE email='saptakds@gmail.com'

Explanation: The column name with ' isn't the value of the column, instead it is the value itself. Maybe you it's a mistake and you want to set the backticks instead of '.

Sebastian Brosch
  • 42,106
  • 15
  • 72
  • 87