Warning: mysql_fetch_array() - Building my own tags

Discussion in 'PHP' started by zaay, Aug 7, 2009.

  1. #1
    Hello everyone !

    I have problems with my tags script !!
    Please i need help with it :
    Code:

    function pokaziZnacke($tag) {
        
        global $db;
        
        
    $query = "SELECT * FROM news WHERE tags=$tag";
     $znacke_rezultat = mysql_query ($query);
     
        /* prikaze znacke */
    
        while ($row = mysql_fetch_array ($znacke_rezultat)) {
            
        $tags = htmlentities ($row['tags']);
        $title = htmlentities ($row['title']);
        $news = nl2br (strip_tags ($row['newstext'], '<a><b><i><u>'));
        
        echo "News which have this tag";
        
        echo " ".$title." ";
        
     
        }
    }
         
    PHP:
    My warning say it's problem in line 17 which is :
        while ($row = mysql_fetch_array ($znacke_rezultat)) {
    PHP:
    Total error:
    Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/keglichh/public_html/mysite.com/functions/tags.php on line 17
     
    zaay, Aug 7, 2009 IP
  2. oop

    oop Peon

    Messages:
    35
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Change
    $query = "SELECT * FROM news WHERE tags=$tag";
    PHP:
    TO

    $query = mysql_query("SELECT * FROM news WHERE tags=$tag");
    PHP:
     
    oop, Aug 7, 2009 IP
  3. zaay

    zaay Member

    Messages:
    269
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    30
    #3
    Hi !

    Thanx for help, but still getting same error :(
     
    zaay, Aug 7, 2009 IP
  4. oop

    oop Peon

    Messages:
    35
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    this should be working now

    $query = mysql_query("SELECT * FROM news WHERE tags='".mysql_real_escape_string($tag)."'");
     
    oop, Aug 7, 2009 IP
  5. zaay

    zaay Member

    Messages:
    269
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    30
    #5
    Damn.. Sorry oop, still same problem :S :(
     
    zaay, Aug 7, 2009 IP
  6. oop

    oop Peon

    Messages:
    35
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    hmm lmao its right !!!! whats the post the error and the code you have now!
     
    oop, Aug 7, 2009 IP
  7. zaay

    zaay Member

    Messages:
    269
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    30
    #7
    Error
    Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/keglichh/public_html/mysite.com/functions/tags.php on line 18

    Line 18:
        while ($row = mysql_fetch_array ($znacke_rezultat)) {
    PHP:

    Total code:
    function pokaziZnacke($tag) {
       
        global $db;
       
      	
    $query = mysql_query("SELECT * FROM news WHERE tags='".mysql_real_escape_string($tag)."'"); 
    $query = mysql_query($query); 
     $znacke_rezultat = mysql_query ($query);
     
        /* prikaze znacke */
    
        while ($row = mysql_fetch_array ($znacke_rezultat)) {
           
        $tags = htmlentities ($row['tags']);
        $title = htmlentities ($row['title']);
        $news = nl2br (strip_tags ($row['newstext'], '<a><b><i><u>'));
       
        echo "News which have this tag";
       
        echo " ".$title." ";
       
     
        }
    }
    PHP:
     
    zaay, Aug 7, 2009 IP
  8. oop

    oop Peon

    Messages:
    35
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #8
    
    
    function pokaziZnacke($tag) { 
        $query = mysql_query("SELECT * FROM news WHERE tags='".mysql_real_escape_string($tag)."'"); 
        $query = mysql_query($query);
    
        if(!is_resource($query)){die('MySql must not be connected!');}
     
        /* prikaze znacke */
    
        while ($row = mysql_fetch_assoc($query)) {
           
            $tags = htmlentities($row['tags']);
            $title = htmlentities($row['title']);
            $news = nl2br(strip_tags($row['newstext']));
       
            echo "News which have this tag: ".$title;
       
     
        }
    }
    
    
    PHP:
     
    oop, Aug 7, 2009 IP
  9. zaay

    zaay Member

    Messages:
    269
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    30
    #9
    MySql must not be connected! error :( because you deleted global db which generate Database
     
    zaay, Aug 7, 2009 IP
  10. oop

    oop Peon

    Messages:
    35
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #10
    
    function pokaziZnacke($tag) { 
        $query = mysql_query("SELECT * FROM news WHERE tags='".mysql_real_escape_string($tag)."'") or die(mysql_error()); 
        $query = mysql_query($query);
    
        if(!is_resource($query)){die('MySql must not be connected!');}
     
        /* prikaze znacke */
    
        while ($row = mysql_fetch_assoc($query)) {
           
            $tags = htmlentities($row['tags']);
            $title = htmlentities($row['title']);
            $news = nl2br(strip_tags($row['newstext']));
       
            echo "News which have this tag: ".$title;
       
     
        }
    }
    
    PHP:
    OR

    then make sure your connected to your database !!
     
    oop, Aug 7, 2009 IP
  11. zaay

    zaay Member

    Messages:
    269
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    30
    #11
    Hi!

    I am connect to database...
    
    function pokaziZnacke($tag) {
    	
    	global $db;
    	
    	
        $query = mysql_query("SELECT * FROM news WHERE tags='".mysql_real_escape_string($tag)."'") or die(mysql_error());
        $query = mysql_query($query);
    
        if(!is_resource($query)){die('MySql must not be connected!');}
     
        /* prikaze znacke */
    
        while ($row = mysql_fetch_assoc($query)) {
           
            $tags = htmlentities($row['tags']);
            $title = htmlentities($row['title']);
            $news = nl2br(strip_tags($row['newstext']));
       
            echo "News which have this tag: ".$title;
       
     
        }
    }
    PHP:
    Other function which need database is working and it's same: global $db;
     
    zaay, Aug 7, 2009 IP
  12. oop

    oop Peon

    Messages:
    35
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #12
    you dont need to pull your DB connection into functions its a kinda SUPER global and therefore you dont need global.. run the last one i sen you and tell me the mysql error it should show
     
    oop, Aug 7, 2009 IP
  13. zaay

    zaay Member

    Messages:
    269
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    30
    #13
    That wihout db connection gives same error:
    MySql must not be connected
     
    zaay, Aug 7, 2009 IP
  14. oop

    oop Peon

    Messages:
    35
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #14
    
    function pokaziZnacke($tag) {
        
        global $db;
        
        
        $query = mysql_query("SELECT * FROM news WHERE tags='".mysql_real_escape_string($tag)."'");
        $query = mysql_query($query) or die(mysql_error());
    
        if(!is_resource($query)){die('MySql must not be connected!');}
     
        /* prikaze znacke */
    
    
        while ($row = mysql_fetch_assoc($query)) {
           
            $tags = htmlentities($row['tags']);
            $title = htmlentities($row['title']);
            $news = nl2br(strip_tags($row['newstext']));
       
            echo "News which have this tag: ".$title;
       
     
        }
    }
    
    PHP:
    that should tell you your error
     
    oop, Aug 7, 2009 IP
  15. zaay

    zaay Member

    Messages:
    269
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    30
    #15
    Damn, what is that now :D

    You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Resource id #5' at line 1
     
    zaay, Aug 7, 2009 IP
  16. Bohra

    Bohra Prominent Member

    Messages:
    12,573
    Likes Received:
    537
    Best Answers:
    0
    Trophy Points:
    310
    #16
    Whats ur current code i see soo many codes in this thread dont know which 1 to trouble shoot
     
    Bohra, Aug 7, 2009 IP
  17. oop

    oop Peon

    Messages:
    35
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #17
    theres to mysql_queries !!


    try do ->

    
    function pokaziZnacke($tag) {
           
        $query = sprintf("SELECT * FROM news WHERE tags = '%s'",mysql_real_escape_string($tag));
        $query = mysql_query($query) or die(mysql_error());
    
        if(!is_resource($query)){die('MySql must not be connected!');}
     
        /* prikaze znacke */
    
    
        while ($row = mysql_fetch_assoc($query)) {
           
            $tags = htmlentities($row['tags']);
            $title = htmlentities($row['title']);
            $news = nl2br(strip_tags($row['newstext']));
       
            echo "News which have this tag: ".$title;
       
     
        }
    
    PHP:
     
    oop, Aug 7, 2009 IP
  18. zaay

    zaay Member

    Messages:
    269
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    30
    #18
    waaa oop is working nowww ! ;) Thanx! Can you please explain me what did you do :$

    Ok another question.. I just planned to start working on my tags system for my news.. OK problem is .. When i am adding news, i have input field for tags.. So i write them like that: tag,tag2 ... And now problem is.. Tags for this news are tag1,tag2 :D not tag1 and tag2 :D Well if you don't udnerstand me, i cannot get each tag from database.. So that my system will show tags and when you click on one tag it will show news with that tag. Now one of my news have these tags: testing,tags. So actually both tags are one record.. i want each tag after "," as one record which belong to news ID :D lol :d
     
    zaay, Aug 7, 2009 IP
  19. oop

    oop Peon

    Messages:
    35
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #19
    //ok lets say u have a form with an input box named tags, on the add post functions you need to do summat like
    
    $Tags = trim($_POST['tags']);
    
    //now we want to split them up so we do this
    
    $SplitTags = array_walk(explode(',',trim($Tags)),'trim'); // this wil make all tags seperate and trim each one
    
    //then you have an aray of tags :P
    
    PHP:
     
    oop, Aug 7, 2009 IP
  20. SHOwnsYou

    SHOwnsYou Peon

    Messages:
    209
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #20
    The problem was that your query was running twice.

    $query = mysql_query("SELECT * FROM news WHERE tags='".mysql_real_escape_string($tag)."'");
    $query = mysql_query($query) or die(mysql_error());

    So the second $query is actually running mysql_query(mysql_query("SELECT * FROM NEWS WHERE tags='".mysql_real_escape_string($tag)."'");

    That is not a valid query.

    If you changed the first $query to just: $query = "SELECT * FROM `news` WHERE `tags` = '$tag'";
    It would also work. You do still want to escape $tag, however.
     
    SHOwnsYou, Aug 7, 2009 IP