简体   繁体   中英

MySQL Query in PHP won't print results

This is my php script for the query:

    $query="SELECT f.EVENT, 
    f.LOCATION, 
    f.DOE, 
    f.CATEGORY, 
    f.LOGLOC, 
    f.LOGDOE, 
    f.LOGCAT, 
    ((( f.NUMOFTIMESLOC * ( Log(f.LOGLOC) ) ) + 
                                             ( f.NUMOFTIMESDOE * ( Log(f.LOGDOE) ) ) + ( 
                                           f.NUMOFTIMESCAT * (Log(f.LOGCAT))))) AS FINALRANK 
                                FROM   (SELECT E.EVENT, 
                                               ED.LOCATION, 
                                               ED.DOE, 
                                               ED.CATEGORY, 
                                               ( ( ED.NUMOFDATASET / ED.NUMOFTIMESLOC ) * 100 ) AS LOGLOC, 
                                               ( ( ED.NUMOFDATASET / ED.NUMOFTIMESDOE ) * 100 ) AS LOGDOE, 
                                               ( ( ED.NUMOFDATASET / ED.NUMOFTIMESCAT ) * 100 ) AS LOGCAT, 
                                               ED.NUMOFTIMESLOC, 
                                               ED.NUMOFTIMESDOE, 
                                               ED.NUMOFTIMESCAT 
                                        FROM   EVENT E, 
                                               EVENTDETAIL ED 

                                        WHERE  E.EVENTID = ED.EVENTID 
                                               AND ED.LOCATION = 'Liverpool' 
                                               AND ED.CATEGORY = 'Concerts') f ORDER BY FINALRANK";
                                $result=mysql_query($query);
                                $num=mysql_numrows($result);
                                mysql_close();
                                $i=0;
                                if(!$num){
                                    echo ("No Results");
                                }
                                else{
                                    while ($i < $num) {
                                        $eventid=mysql_result($result,$i,"eventid");
                                        echo $eventid;
                                        echo"<br>";
                                        $i++;
                                    }
                                }

When this query is run in command controller it works fine and prints 2 results for the query but when used with PHP it just returns 'No Results'. Please Help!

  • your problem is that you are closing the connection by mysql_close(); before you output results

remove that ligne of mysql_close(); and make it in the last of your results.

  • and also make sure to change this mysql_numrows to mysql_num_rows

I think this is because of mysql_numrows() . This should be mysql_num_rows() . Change it here.

$num=mysql_num_rows($result);
$num=mysql_numrows($result);  

that is wrong function name, it is

$num=mysql_num_rows($result);

mysql_num_rows

That way your $num will not be false and you will not see that "No Results" message; Currently due to that mistake it is being set false and hence your if always triggers

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM