15

I'm fairly new to PHP and I've been looking around and can't seem to find the specific answer I'm looking for.

I want to make a SQL query, such as this:

$result = mysqli_query($connection, $command)
if (!$result) { die("Query Failed."); }

// Create my array here ... I'm thinking of maybe having to
// make a class that can hold everything I need, but I dunno    

while($row = mysqli_fetch_array($result))
{
    // Put the row into an array or class here...
}

mysqli_close($connection);

// return my array or class

Basically I want to take the entire contents of the result and create an array that I can access in a similar fashion as the row. For example, if I have a field called 'uid' I want to be able to get that via myData['uid']. I guess since there could be several rows, maybe something more like myData[0]['uid'], myData[1]['uid'], etc.

Any help would be appreciated.

4
  • what is you db structure ... ? you can do like 4myData['id'] = $row['id']; Commented Jun 12, 2013 at 1:28
  • But what if I don't know how many rows or fields will be returned? I also am attempting to make a global SQL class that can retrieve data for me, so I won't even know the names of the fields so I can't assign like that. My DB structure right now is just uid:int firstName:varchar(64) lastName:varchar(64) emailAddress:varchar(64) password:varchar(64) Commented Jun 12, 2013 at 1:44
  • you can limit result in query Commented Jun 12, 2013 at 1:46
  • This is what I'm attempting to do: call something like SQL::Get("SELECT uid FROM users WHERE email='".$emailAddress."'"); and have an array where I could just do $result[0]['uid'], or SQL::Get("SELECT firstName,lastName FROM users WHERE email='".$emailAddress."'"); and have an array where I could call $result[3]['firstName'] Commented Jun 12, 2013 at 1:48

4 Answers 4

24

You can do:

$rows = [];
while($row = mysqli_fetch_array($result))
{
    $rows[] = $row;
}
Sign up to request clarification or add additional context in comments.

3 Comments

That doesn't work. I just attempted that and then did an echo($rows[0]['uid']) and got nothing...
Got it to work. I was doing $array[] = $row, but I actually needed to just do $array = $row.
with me not work with $rows = []; need to change to $rows = array();
6

You might try to use mysqli_result::fetch_all() for arrays:

$result = mysqli_query($connection, $command)

if (!$result) { die("Query Failed."); }

$array = $result->fetch_all();

$result->free();

mysqli_close($connection);

NOTE: This works with MySQLND only.


For class, you might try to use something like this:

$result = mysqli_query($connection, $command)

if (!$result) { die("Query Failed."); }

while($model = $result->fetch_assoc()){
    // Assuming ModelClass is declared
    // And have method push() to append rows.
    ModelClass::push($model);
}

$result->free();

mysqli_close($connection);

OR this:

// Base Model - abstract model class.
class ModelClass extends BaseModel {

    // constructor
    public function __construct(mysqli &$dbms){
        // $this->dbms is MySQLi connection instance.
        $this->dbms   = &$dbms;

        // $this->models is buffer for resultset.
        $this->models = array();
    }

    // destructor
    public function __destruct(){
        unset($this->dbms, $this->models);
    }

    public function read(){            
        $result = $this->dbms->query($command);

        if($this->dbms->errno){
            throw new Exception($this->dbms->error, $this->dbms->errno);
        }

        $this->models = $result->fetch_all();

        $result->free();
    }
}

4 Comments

@NullPoiиteя unfortunately, it not fits with Linux. I burned last time, when I was using it.
would you give documentation where they are saying this will only work with windows os only because afaik it should work with linux also ... :)
@NullPoiиteя yes... Not knew it is possible to install MySQLND on Linux.
mysqli has limitation and that sucks .... that is why most of us suggest to use pdo mysql
1
//object oriented style mysqli
//connect to your db
$mysqli = new mysqli("host", "user", "password", "dbname");
$result = $mysqli->query("SELECT * FROM `table`");

//use mysqli->affected_rows
for ($x = 1; $x <= $mysqli->affected_rows; $x++) {
    $rows[] = $result->fetch_assoc();
}

//this will return a nested array
echo "<pre>";
print_r($rows);
echo "</pre>";

edit this and put it on a class and just call it anytime you're going to make a query with your database.

Comments

1

fetch_array: https://www.php.net/manual/en/mysqli-result.fetch-array.php

$result = $mysqli_connection->query($sql);
$row = $result->fetch_array(MYSQLI_ASSOC);
            print_r($row);

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.