How do I query select all my mysql rows under the modification of a stored function with a reddit-based algorithm?

StackOverflow https://stackoverflow.com/questions/8830092

Question

I've spent the whole day googling and deleting and inserting trying to implement this code. I've been trying to implement a reddit-like site using php and mysql. I have been following another question: PHP MYSQL Query Algorithm Help and it works very well and ranks rows according to the algorithm coded in the previous question within myphpadmin when I query a stored function

SELECT
*,
reddit_rank(`time_added`, `up_votes`, `down_votes`) as rank
FROM
`table`
ORDER BY
rank;

, but when I paste the query into my php file:

<?php 
include("config.php");
$q= "SELECT *,reddit_rank(`time` , `votes_up` , `votes_down`) FROM `wallposts` ORDER BY rank LIMIT 0 , 30";
$r = mysql_query($q);
if(mysql_num_rows($r) > 0) {
while($row = mysql_fetch_assoc($r)){
...?>

It doesn't work and I get a white HTML screen. So for example in my PHP when I have

$q = "SELECT * FROM wallposts ORDER BY votes_up DESC"; 

my reddit/facebook-like wall has prepended each of my rows from mysql and everything works just fine. but when i change it to

$q= "SELECT *,reddit_rank(`time` , `votes_up` , `votes_down`) FROM `wallposts` ORDER BY rank LIMIT 0 , 30"; 

the webpage returns nothing but a white screen even though I know it works in myphpadmin.

Is there something wrong with my syntax or is it not possible to query a select all with a stored function to order the results in php?

Was it helpful?

Solution

I think I found a solution by creating a view and then querying that view instead of the original table. After I queried the stored function in myphpadmin:

SELECT
*,
reddit_rank(`time_added`, `up_votes`, `down_votes`) as rank
FROM
`table`
ORDER BY
rank; 

I then created a view after those results were returned. Then instead of querying the same way in my PHP file, I queried the new mysql view with:

SELECT * FROM [view] ORDER BY rank

Voila!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top