2

I have this code...

$results_query = "SELECT * FROM profile WHERE string LIKE '%" . $search_deliminator . "%'";
$results_result = $database->query($results_query);
$result = mysql_fetch_array($results_result);

What I'm trying to do is pull all the rows in which the string column contains my search deliminator. Further, I would like to get the values from each column of the rows.

How would I pull the multidimensional array I need containing each row, and each value of each column within each row?

EDIT:

I'm looking to do something to this effect...

while ($row = mysql_fetch_assoc($results_result)) {

    $result[] = $row;

}

Then echo each column like this...

foreach ($result as $row) {
    echo $row["0"];
}
8
  • What database class are you using? Commented May 22, 2015 at 15:24
  • Are you using mysql_, mysqli, or PDO? I havent seen the mysql_ driver used as an object before. Commented May 22, 2015 at 15:29
  • The class just handles the connection. I totally forgot about explaining that. I've just never used anything other than simple one-column queries. I think what I'm having a hard time understanding is what to use in the $result variable. Commented May 22, 2015 at 15:29
  • 1
    This is basic foreach looping: foreach($result as $key => $value){ //do some stuff with $value which now contains your row in an array where each //column is an index } Commented May 22, 2015 at 15:37
  • 1
    The column name is the key in the foreach loop. Commented May 22, 2015 at 15:48

3 Answers 3

5

To get a flattened array of the result set, try this:

$result = array();

while ($row = mysql_fetch_assoc($results_result)) {
    $result[$row['id']] = $row;
}

echo json_encode($result);

Also, you should look into either MySQLi or PDO to replace the (now deprecated) MySQL extension.

Sign up to request clarification or add additional context in comments.

3 Comments

I'm unsure what a flattened array is. Could I use something like, while ($row = mysql_fetch_assoc($results_result)) { $result[] = $row; } and then use a for each loop? I.E., foreach ($result as $a_row) { echo $a_row["0"]; }
See question edit for a better description of what I'm looking for.
If you're just looking to output the data, that's best done directly in the while loop. However, if you want to store the MySQL results in a variable so that it can be used elsewhere, using a while loop to build that variable would work fine.
2

If you use PDO, you can use the fetchAll() method:

$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);

With mysql or mysqli, you use a loop:

$rows = array();
while ($row = $stmt->fetch_assoc()) {
    $rows[] = $row;
}

Comments

1

try this

$i=0;
while ($row = mysql_fetch_assoc($results_result)) {

 $result[$i]["column1"] = $row["column1"];
 $result[$i]["column2"] = $row["column2"];
 $i++;
}

To display the output use:

foreach ($result as $row) {
 echo $row["column1"];
 echo $row["column2"];
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.