PHP Query + IF ELSE

时间:2011-12-23 09:41:49

标签: php mysql if-statement

我有一个PHP文件正在进行查询

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>";
        }
    }       
}

我已经在PHPMyAdmin中测试了查询,但是我得到了一个结果,但是当我在页面中运行该函数时,它只会显示“没有相关链接”的声明吗?

2 个答案:

答案 0 :(得分:4)

这是固定代码。 :)问题是你在资源上使用了mysql_query(第一个mysql_query),这将给出0行或异常..

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>";
        }
    }       
}

答案 1 :(得分:0)

将您的功能更改为(更改为行)

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($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>";
        }
    }       
}