0

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?

5
  • 1
    first you have a big fat SQL injection in your script. Commented Dec 23, 2011 at 9:43
  • I know but it's a closed system so that's not to much of an issue Commented Dec 23, 2011 at 9:44
  • Can you please post the output of var_dump($solnid);? Commented Dec 23, 2011 at 9:46
  • have you tried mysql_fetch_assoc($result) ??? Commented Dec 23, 2011 at 9:46
  • closed system: 80% of attacks comes from collaborators plus it's a bad habit Commented Dec 23, 2011 at 9:52

2 Answers 2

4

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>";
        }
    }       
}
Sign up to request clarification or add additional context in comments.

2 Comments

You should also sanitize the parameter for $solnid, it is possible to exploit that SQL as it is right now and do some injects. :)
Nice catch! I totally missed the double mysql_query when I read through it.
0

change your function to (change to lines)

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.