Skip to content
Advertisement

MySQLI Fetch Database Column

I have database table options, where am using it as a dedicated table to store information. I have never heard of this type of table query and have never tried it. I saw on WordPress database so thought to try my own.

Here’s database table

enter image description here

This is what am trying now

$sql = "SELECT * FROM options WHERE option_flag = 'settings'";
var_dump( $db->FETCH_FIELD($sql) ); 

This is inside my database class file $db

public function FETCH_FIELD($sql){
    $this->results = mysqli_query($this->link, $sql);       
$rows = mysqli_fetch_fields( $this->results );
    return $rows;
}

The Problem

When I var_dump the results, I don’t see any database information.

enter image description here

Advertisement

Answer

Since you want to get an array that shows the query results, I would try this:

<?php
$db = new mysqli(HOST, USERNAME, PASSWORD, DBNAME);
$stmt = $db->query("SELECT * FROM options WHERE option_flag = 'settings'");
$result = $stmt->fetch_all();
var_dump($result);

Of course, this works in procedural style, too:

<?php
$link = mysqli_connect(HOST, USERNAME, PASSWORD, DBNAME);
$stmt = mysqli_query($link, "SELECT * FROM options WHERE option_flag = 'settings'");
$result = mysqli_fetch_all($stmt);
var_dump($result);

Go to the php documentation to learn more about mysqli_result::fetch_all.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement