PHP Query Problem/Error

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ziycon
    Contributor
    • Sep 2008
    • 384

    PHP Query Problem/Error

    I'm getting the error below, i've printed out the SQL query and its fine, it also works in the DB, SELECT sub_id,type FROM comments WHERE id='103' LIMIT 1, any ideas?
    Warning: mysql_fetch_arr ay(): supplied argument is not a valid MySQL result resource in C:\Apache\htdoc s\config\sql.ph p on line 798
    This is the code:
    Code:
    function getCommentLink($id) {
      $link = "";
      $type = "";
      
      $sql = "SELECT sub_id,type FROM comments WHERE id='".$id."' LIMIT 1";
      echo $sql;
      $sql = mysql_query($sql);
      while ($row = mysql_fetch_array($sql)) //Line 798
      {
        $type = $row['type'];
        $sql = mysql_query("SELECT id,title FROM ".$row['type']." WHERE id='".$row['sub_id']."' LIMIT 1");
        while ($row = mysql_fetch_array($sql))
        {
          if($type == "news")
          {
            $type = "article-news";
          }
          else if($type == "reviews") 
          {
            $type = "article-reviews";
          }
          else if($type == "previews") 
          {
            $type = "article-previews";
          }
          
          $link = "../../../".$type."/".$row['id']."-".preg_replace('#[^a-zA-Z0-9]+#','-',$row['title']).".htm";
        }
      }
      return $link;
    }
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    Is 'sub_id' and INT column? If so, you shouldn't have quotes around the value.

    Code:
    $sql = "SELECT sub_id,type FROM comments WHERE id={$id}"
    Thanks,
    Markus.

    Comment

    • ziycon
      Contributor
      • Sep 2008
      • 384

      #3
      It is an int column but i have commas around it for extra security against SQL injection attacks.

      Got it sorted, had the db connect function after the sql statement.

      Comment

      • Markus
        Recognized Expert Expert
        • Jun 2007
        • 6092

        #4
        Line 27 is just ugly, ugly, ugly, btw.

        You should have a couple of constants defined in your application that point to various locations. That way, you don't need to use '../', etc,. to find the right directory. Something like..

        Code:
        define("ART_PATH", "/html/your-site.com/html_docs/articles/");
        Then you can get to 'articles' easily and neatly.

        Thanks,
        Markus.

        Comment

        • ziycon
          Contributor
          • Sep 2008
          • 384

          #5
          I know its terrible, I'm going to sort all those constants out once i get the system working properly, just a bit of jigging about, thanks for the feedback.

          Comment

          • Markus
            Recognized Expert Expert
            • Jun 2007
            • 6092

            #6
            Originally posted by ziycon
            I know its terrible, I'm going to sort all those constants out once i get the system working properly, just a bit of jigging about, thanks for the feedback.
            Jolly good!

            Glad you got it sorted,
            Markus.

            Comment

            Working...