Jump to content


Photo

Getting data out of a MYSQL_ASSOC query ... newbie


  • Please log in to reply
2 replies to this topic

#1 blakekr

blakekr
  • Members
  • PipPip
  • Member
  • 19 posts

Posted 27 August 2006 - 03:07 PM

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

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:

       $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?

#2 jvalarta

jvalarta
  • Members
  • PipPipPip
  • Advanced Member
  • 42 posts

Posted 27 August 2006 - 04:49 PM

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:

$sql = "SELECT * FROM table";
	$result = mysql_query($sql);
	while ($row = mysql_fetch_array($result)) {
		extract($row);
	}


#3 blakekr

blakekr
  • Members
  • PipPip
  • Member
  • 19 posts

Posted 28 August 2006 - 05:42 PM

Thanks, I greatly appreciate it and that seems to have gotten me over the hump!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users