I have code that stores the results of a mysqli query in an array, but I want to make the code a little more advanced by only storing the results of one column in the array. Here's my code:

// Create array of devices that match the current unit in array $unitsarray
$result = $mysqli->query("SELECT * FROM `department devices` WHERE unit LIKE $unit");

//Fetch all rows in result and store them in an array $rows
$rows = array();
while($row = $result->fetch_row()) {
    $rows[] = $row;
}
share|improve this question

68% accept rate
Voting to close becaue OP has made no effort to try anything himself - you don't want "to make the code a little more advanced", you want somebody to make it a little more advanced for you – Mark Baker 23 hours ago
I have obviously tried, and I can't figure it out, that's why I am asking here. – Jon Erickson 23 hours ago
Well, if you only want to store one column, you could always just select that column... i.e. SELECT mycol FROM... – Daren Chandisingh 23 hours ago
What do mean store the results of one column in the array? If you just want one column from your row then select that column in your SQL. How does this make anything more advanced? – GoogleGuy 23 hours ago
@Jon - the code you've posted gives no indication that you've tried anything... tell us what you tried, and we're more likley to spend our time helping – Mark Baker 23 hours ago
show 1 more comment
feedback

closed as not a real question by Mark Baker, TheShiftExchange, valex, bažmegakapa, rekire 8 hours ago

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, see the FAQ.

2 Answers

up vote 2 down vote accepted

Ex: If you want column x, You just get the column x from your database so, did you try to change your query;

$result = $mysqli->query("SELECT x FROM `department devices` WHERE unit LIKE $unit");
share|improve this answer
feedback

What would make your code "more advanced" is to:

  1. accept that '->fetch_assoc()` always returns an array of the columns
  2. write your own database handler to handle the sort of resultset transformations you want.

The latter is called abstraction and is a major reason code libraries are written.

share|improve this answer
feedback

Not the answer you're looking for? Browse other questions tagged or ask your own question.