SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    SitePoint Enthusiast ante's Avatar
    Join Date
    Aug 2004
    Location
    Split, Croatia
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Search code - what's wrong?

    Hello,
    could somebody tell me what's wrong with this code:

    PHP Code:
    $c=mysql_query("SELECT * FROM forum_postext WHERE post_text LIKE '%$search%'") or die(mysql_error());
    while (
    $d=mysql_fetch_array($c))
    {
     
    $post_id2=$d["post_id"];
    // echo $post_id2 prints id
     
       
    $e=mysql_query("SELECT * FROM forum_post WHERE post_id='$post_id2'") or die(mysql_error());
    // there is no mysql error
     
       
    while ($f=mysql_fetch_array($e));
       {
        echo 
    $f["topic_id"];
     
    // prints nothing :(
    ...
    ...
    ... 
    Thank you,
    Ante

  2. #2
    SitePoint Evangelist mrwooster's Avatar
    Join Date
    Jan 2006
    Posts
    518
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try changing this line:

    PHP Code:
    <?php
    $e
    =mysql_query("SELECT * FROM forum_post WHERE post_id='$post_id2'") or die(mysql_error());
    // there is no mysql error
    ?>
    to this:

    PHP Code:
    <?php
    $e
    =mysql_query("SELECT * FROM forum_post WHERE post_id='".$post_id2."'") or die(mysql_error());
    // there is no mysql error
    ?>
    Its just a guess, but it is the sort of thing I have had a lot of problems with in the past.

    If that does not fix it, try something like this:

    PHP Code:
    <?php
    $query 
    "SELECT * FROM forum_post WHERE post_id='".$post_id2."'";
    echo 
    $query;
    ?>

    Regards

    Guy

  3. #3
    SitePoint Enthusiast ante's Avatar
    Join Date
    Aug 2004
    Location
    Split, Croatia
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    D'oh!

    Still doesn't output anything

  4. #4
    SitePoint Enthusiast ante's Avatar
    Join Date
    Aug 2004
    Location
    Split, Croatia
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    But I think that query is not problem because if I put this:
    PHP Code:
    $e=mysql_query("SELECT * FROM forum_post WHERE post_id='".$post_id2."'") or die(mysql_error());
       while (
    $f=mysql_fetch_array($e));
       {
        echo 
    $f["topic_id"];
        echo 
    "<br />";
    ... 
    It prints only breaks, but not topic_id... Hmm...

  5. #5
    SitePoint Evangelist mrwooster's Avatar
    Join Date
    Jan 2006
    Posts
    518
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    can your print_r the $f array:

    PHP Code:
    <?php
    $e
    =mysql_query("SELECT * FROM forum_post WHERE post_id='".$post_id2."'") or die(mysql_error());
       
    $f=mysql_fetch_array($e)
      
    print_r($f);

    ?>
    Do you get anything from that?

  6. #6
    SitePoint Enthusiast ante's Avatar
    Join Date
    Aug 2004
    Location
    Split, Croatia
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Nothing...

  7. #7
    SitePoint Evangelist mrwooster's Avatar
    Join Date
    Jan 2006
    Posts
    518
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That means that the query is not working.

    Are you using phpMyAdmin? If so try this:

    PHP Code:
    <?php
    $query 
    "SELECT * FROM forum_post WHERE post_id='".$post_id2."'";
    echo 
    $query;
    ?>
    And then manually query the database using the SQL function in phpmyadmin, see what you get.

    The fact the print_r($f) is not outputting anything means that the array is not being created in mysql_fetch_array, but you are not getting any errors from the query. Try the above to make sure that the query is returning the expected results.

    Off Topic:


    BTW: Just noticed you are from Split, spent a couple of weeks there last summer, really great place.

  8. #8
    SitePoint Enthusiast ante's Avatar
    Join Date
    Aug 2004
    Location
    Split, Croatia
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by mrwooster View Post
    That means that the query is not working.

    Are you using phpMyAdmin? If so try this:

    PHP Code:
    <?php
    $query 
    "SELECT * FROM forum_post WHERE post_id='".$post_id2."'";
    echo 
    $query;
    ?>
    And then manually query the database using the SQL function in phpmyadmin, see what you get.

    The fact the print_r($f) is not outputting anything means that the array is not being created in mysql_fetch_array, but you are not getting any errors from the query. Try the above to make sure that the query is returning the expected results.

    Off Topic:


    BTW: Just noticed you are from Split, spent a couple of weeks there last summer, really great place.


    with that code it outputs:Resource id #.... etc.

    I didn't understood part about phpmyadmin; but I hope this is something too? It outputs at least Resource id #8 Resource id #...

    Off Topic:

    Thank you, it is really nice

  9. #9
    SitePoint Evangelist mrwooster's Avatar
    Join Date
    Jan 2006
    Posts
    518
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    AH!!! Try this:

    Is the post_id field a number? If so then remove the quotes:

    PHP Code:
    <?php
    $e
    =mysql_query("SELECT * FROM forum_post WHERE post_id=".$post_id2) or die(mysql_error());
       
    $f=mysql_fetch_array($e)
      
    print_r($f);

    ?>
    I thought that you could use quotes around variables even if they were numbers, but maybe not.

  10. #10
    SitePoint Enthusiast ante's Avatar
    Join Date
    Aug 2004
    Location
    Split, Croatia
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes!
    With:
    PHP Code:
    $e=mysql_query("SELECT * FROM forum_post WHERE post_id=".$post_id2) or die(mysql_error());
    $f=mysql_fetch_array($e);
    print_r($f); 
    Now I get:

    Array ( [0] => 1102 [post_id] => 1102 [1] => ... etc.

    What now? Sorry for bothering you...


  11. #11
    SitePoint Evangelist mrwooster's Avatar
    Join Date
    Jan 2006
    Posts
    518
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK - should have spotted that earlier but obviously not on the ball today.

    You code should look like this:

    PHP Code:
    $c=mysql_query("SELECT * FROM forum_postext WHERE post_text LIKE '%$search%'") or die(mysql_error());
    while (
    $d=mysql_fetch_array($c))
    {
    $post_id2=$d["post_id"];
    // echo $post_id2 prints id

       
    $e=mysql_query("SELECT * FROM forum_post WHERE post_id=".$post_id2) or die(mysql_error());
    // there is no mysql error

       
    while ($f=mysql_fetch_array($e))
       {
        echo 
    $f["topic_id"];

    That should be the correct version of the original code you posted.

    Let me know if you need any more help.

    Regards

    Guy

  12. #12
    SitePoint Enthusiast ante's Avatar
    Join Date
    Aug 2004
    Location
    Split, Croatia
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's it! Thank you!

  13. #13
    SitePoint Enthusiast
    Join Date
    Mar 2007
    Posts
    89
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you should use variables that better describe the information they will contain. Using single letters makes code hard to debug


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •