Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8310149
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T19:21:21+00:00 2026-06-08T19:21:21+00:00

I am getting so frustrated with this. I am trying to make an array

  • 0

I am getting so frustrated with this. I am trying to make an array containing the values of a column using mysqli. Right now when i echo what is returned it is printing nothing. I have tried so many things i can not figure this out.

<?php 
class Database {

    public $dbHost = '';
    public $dbUser = '';
    public $dbPass = '';
    public $dbName = '';

    public $db;

    public function __construct(){ 
        $this->dbConnect();
    }

    public function dbConnect(){

        $this->db = new mysqli($this->dbHost, $this->dbUser, $this->dbPass, $this->dbName);

        /* check connection */
        if (mysqli_connect_errno()){
            printf("Connect failed: %s\n", mysqli_connect_error());
            exit();
        }else{
            echo 'connection made';
        }
    }

    public function q($sql){
        $query = $sql;
        self::preparedStatement($query);
    }

    public function preparedStatement($query){
        $i= 0; //index
        $result = array();

        if ($stmt = $this->db->prepare($query)){ 

            /* execute statement */
            if($stmt->execute()) {
                $stmt->bind_result($name);
                while($stmt->fetch()) {
                    $result[0] = $name;
                }
            } else
                echo "error";

            /* close statement */
            $stmt->close();
        } else {
            echo "Prepare failed: (" . $stmt->errno . ") " . $stmt->error;
        }

        return $result;
    }

    public function __destruct(){}

}


### Test code

$db = new Database();
$res = $db->q("SELECT name FROM test");
echo $res[0];

?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T19:21:23+00:00Added an answer on June 8, 2026 at 7:21 pm

    first of all, remove the 0 in your while loop. You want the values to be stored in an array one after the other, what you’re doing is always overwriting the first value in the array, and you need to store the value you get from fetch() in a temporary array.

    while($row = $stmt->fetch(MYSQLI_ASSOC)) {
       $result[] = $row['name'];
    }
    

    second, you need your q() function to return some value, so change the last statement to

    return self::preparedStatement($query);
    

    Please spend some time Googling about this and looking at examples of how other people are doing it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ok getting frustrated on this one.... Using knockoutJS to do some visual interaction stuff.
Arghhh! I'm getting so frustrated trying to get my head around this WCF service.
I'm getting a little frustrated with requirejs at this point. I'm trying to load
I'm getting really frustrated as I'm trying to get an array to my _Click-Event
Getting super frustrated trying to get this working. Basically this is for a site
So I am getting pretty frustrated with this and feel the only way to
I'm getting pretty frustrated with this, and hope the community can help me out.
Okay...I'm really getting a bit frustrated with this. I know that this post will
I'm getting confused and frustrated after a few hours of trying all kinds of
Getting pretty frustrated with this. I installed pygtk 2.24, pycairo 1.8.10, and pyobject 2.28.3

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.