tags:

views:

119

answers:

5
       $result = mysql_query(" SELECT p.page_url AS url,
                               COUNT(*) AS occurrences 
                               FROM page p, word w, occurrence o
                               WHERE p.page_id = o.page_id AND
                               w.word_id = o.word_id AND
                               w.word_word = \"$keyword\"
                               GROUP BY p.page_id
                               ORDER BY occurrences DESC
                               " );

$output = "<loginsuccess>";

for( $i = 1; $row = mysql_fetch_array($result); $i++ )   {


$output .="<keyword>".$_POST['keyword']."</keyword><name>".$row['url']."</name><occur>".$row['occurrences']."</occur><queryTime>".(substr($end_time-$start_time,0,5))."</queryTime>";
}

$output .= "</loginsuccess>";
print ($output);

I am gettign the output in XML, instead i want it in array... how to achieve this.

The code below is not working...

$ret = array();
for( $i = 1; $row = mysql_fetch_array($result); $i++ )   {

     $tmp['url'] = $row['url'];
     $tmp['occurrences'] = $row['occurrences'];

     $ret[] = $tmp;

}
 return $ret;
+7  A: 

You're already getting the results in an array when you call mysql_fetch_array($result). That's what mysql_fetch_array does.

Unless you want some other kind of array format? If so, you'll have to be more specific.

Welbog
Agreed. You've already got the array, that's what you're using to build the XML. If you don't want the XML... don't build it?
Adam V
$ret = array();for( $i = 1; $row = mysql_fetch_array($result); $i++ ) { $tmp['url'] = $row['url']; $tmp['occurrences'] = $row['occurrences']; $ret[] = $tmp;} return $ret;Can i do something like this.
@theband: Why not just `return mysql_fetch_array($result)`? Why are you overcomplicating things?
Welbog
when i try to return $row... i am not able to get in my flex application the values. girdId.dataprovider = event.result as Array This is not fetching the array values from PHP.
+1  A: 

Use print_r():

echo "<pre>";
print_r($row);
echo "</pre>";

Note: If you won't wrap it in <pre> tags, the output will be difficult to read as it will appear all on one line

John Rasch
A: 

Try it like so, notice the here-doc style of writing the SQL statement. I find it very helpful. Also I would suggest rewriting the SQL to use JOINs instead of crowding the WHERE clause with join criteria.

$sql = <<<EOSQL
SELECT p.page_url AS url, COUNT(*) AS occurrences 
FROM page p, word w, occurrence o
WHERE p.page_id = o.page_id AND
w.word_id = o.word_id AND
w.word_word = "$keyword"
GROUP BY p.page_id
ORDER BY occurrences DESC;
EOSQL;
$result = mysql_query($sql) or die('failed to execute the query');

while ($row = mysql_fetch_array($result)) {
    print_r ($row);
}

Hope this is roughly what you're after

Peter Perháč
+1  A: 

A for-loop is only to be used in an incremental fashion, you attempted to use it in place of a while loop. This isn't "do 1,000 times" but "do while we have a row to use".

Try this:

while($row = mysql_fetch_array($result)) {
  $tmp['url'] = $row['url'];
  $tmp['occurrences'] = $row['occurrences'];
  $ret[] = $tmp;
}
return $ret;
The Wicked Flea
A: 

You need to initialize your $tmp array before using it.

allaryin