Justin Erswell Justin Erswell - 6 months ago 11
MySQL Question

PHP Query + IF ELSE

I Have a PHP file doing a query

function getFiles()
{
$solnid = $_GET[id];
$query = mysql_query("SELECT * FROM library WHERE libr_solutionId = '$solnid' AND libr_deleted IS NULL") or die(mysql_error());
$result = mysql_query($query);
if (mysql_num_rows($result)==0) {
echo "<br>No Related Links";
} else {
while($library = mysql_fetch_assoc($query)) {
echo "<span style=\"margin-bottom: 5px;\"><a href=\"Solutions/image/".$library[libr_solutionref]."/".$library[libr_filename]."\" target=\"_blank\"><img src=\"images/icon-download-02.png\" align=\"absmiddle\" style=\"margin-right: 5px;\"><span>".$library[libr_c_title]."</span></a></span><br>";
echo "<br>";
}
}
}


I have tested the query in PHPMyAdmin and I get a result however when running the function in my page it only displays the 'No Related Links' statement any ideas anyone?

Answer

Here's the fixed code. :) The problem was that you used mysql_query on a Resource (the first mysql_query), and that will give 0 rows, or an exception..

function getFiles()
{
    $solnid = $_GET['id'];
    $result = mysql_query("SELECT * FROM library WHERE libr_solutionId = '$solnid' AND libr_deleted IS NULL") or die(mysql_error());
    if (mysql_num_rows($result)==0) {
        echo "<br>No Related Links";
    } else {
        while($library = mysql_fetch_assoc($result)) {
            echo "<span style=\"margin-bottom: 5px;\"><a href=\"Solutions/image/".$library['libr_solutionref']."/".$library['libr_filename']."\" target=\"_blank\"><img src=\"images/icon-download-02.png\" align=\"absmiddle\" style=\"margin-right: 5px;\"><span>".$library['libr_c_title']."</span></a></span><br>";
            echo "<br>";
        }
    }       
}
Comments