Jump to content

Getting data out of a MYSQL_ASSOC query ... newbie


blakekr

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;

}[/code]

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";
              }
          }[/code]

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?
Link to comment
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)) {
extract($row);
}
[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.