Jump to content


This topic is now archived and is closed to further replies.


Getting data out of a MYSQL_ASSOC query ... newbie

Recommended Posts

I have a search function I'm trying to adapt, which uses a MYSQL_ASSOC query:

[code]function search_perform($terms){
                $result = mysql_query($sql);
                while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
                        $row[score] = 0;
                        foreach($terms_rx as $term_rx){
                                $row[score] += preg_match_all("/$term_rx/i", $row[content_body], $null);
                        $rows[] = $row;
                return $rows;


And when I get the values back, I'm trying to format them, but I can't seem to pull them out of the returned $rows correctly.

This works:

[code]      $result = search_perform($q);
        $output = "We found " . (count($result)) . " results.<P>\n";
        if (count($result)){

          foreach($result as $countit) {
              foreach($countit as $something) {
                      $output .=  $something . "<BR>\n";

But I really need to grab the values in $something one at a time so I can format them.

Trying variations like $output .= $result[0][0] isn't getting at the data, I'm not calling it right.

Hopefully this is easy; any tips?

Share this post

Link to post
Share on other sites
Well, a couple of thoughts:

- You are not required to specific MYSQL_ASSOC in the mysql_fetch_array function. If you take it out, you will be able to call variables both ways, i.e. $var and $row[1]

- I use the extract function (http://www.php.net/extract) and it works great.

Here's a very basic example example:

[code] $sql = "SELECT * FROM table";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)) {

Share this post

Link to post
Share on other sites


Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.