Than Htut Oo Than Htut Oo - 9 months ago 24
MySQL Question

Why displaying array when select column in mysql?

I am trying to select a column in mysql in php,

function PageController() {

$data = [
'categories' => _db_get_select("categories", ['name'])

];
load_view("tutu", $data);


and

function _db_get_select($table_name, $columns) {
$servername = _c("database.server_name");
$username = _c("database.username");
$password = _c("database.password");
$dbname = _c("database.db_name");

$conn = mysqli_connect($servername, $username, $password, $dbname);

if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// echo "Connected successfully";

$sql = "SELECT name=$columns FROM . $table_name.";
var_dump($sql);


the result is displaying like this


string(42) "SELECT ['name']=Array FROM . categories."


I want to be like this


SELECT name FROM . categories.


Thanks in advance.

Answer Source

You may use this script

function PageController() {
$data = [
    'categories'    => _db_get_select("categories", "name")
];
load_view("tutu", $data);

And the function will be

function _db_get_select($table_name, $columns) {
$servername = _c("database.server_name");
$username = _c("database.username");
$password = _c("database.password");
$dbname = _c("database.db_name");
$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}
 $sql = "SELECT {$columns} FROM  {$table_name}";
}