Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource

Discussion in 'MySQL' started by centralexpert, Mar 1, 2008.

  1. #1
    Hello,

    I get the following error message after using the following script:

    Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in

    <?
    //connect to mysql
    //change user and password to your mySQL name and password
    {
    include("../includes/dbconnect.php");
    }	
    //select which database you want to edit
    mysql_select_db("my_db"); 
    
    $search=$_POST["search"];
    
    //get the mysql and store them in $result
    //change whatevertable to the mysql table you're using
    //change whatevercolumn to the column in the table you want to search
    $result = mysql_query("SELECT * FROM downloads WHERE message LIKE '%$search%'");
    
    //grab all the content
    while($row = mysql_fetch_array($result))
    {	
       //the format is $variable = $r["nameofmysqlcolumn"]
       //modify these to match your mysql table columns
      
       $title=$row["title"];
       $description=$row["description"];
       $filesize=$row["filesize"];
       $url=$row["url"];
       $cid=$row["cid"];
       
       //display the row
       echo "$title <br> $description <br> $filesize <br> $url <br> $cid <br>";
    }
    ?>
    Code (markup):
    Any help would be appreciated.

    Regards
     
    centralexpert, Mar 1, 2008 IP
  2. farooqaaa

    farooqaaa Well-Known Member

    Messages:
    2,330
    Likes Received:
    149
    Best Answers:
    0
    Trophy Points:
    180
    #2
    Something wrong with MySQL Connect OR Selecting the DB? OR:

    Try using this result:
    
    $result = mysql_query("SELECT * FROM downloads WHERE message LIKE '%$search%'") or die (mysql_error());
    
    Code (markup):
    And see what error does it show.
     
    farooqaaa, Mar 1, 2008 IP
  3. centralexpert

    centralexpert Peon

    Messages:
    27
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Hi,

    Thanks for your quick responce.

    I'll try your suggestion and let you know
     
    centralexpert, Mar 1, 2008 IP
  4. centralexpert

    centralexpert Peon

    Messages:
    27
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    It turns out it's my lack of knowledge of PHP/MySQL.

    $result = mysql_query("SELECT * FROM downloads WHERE [COLOR="Red"]message[/COLOR] LIKE '%$search%'");
    Code (markup):
    Message isn't a table in my db. :p

    So i've used title instead

    Now when I submit a search it reveals nothing, however i know there is data in the tables matching the search, but it reveals nothing.

    Thanks again
     
    centralexpert, Mar 1, 2008 IP
  5. aaron_nimocks

    aaron_nimocks Im kind of a big deal Staff

    Messages:
    5,563
    Likes Received:
    627
    Best Answers:
    0
    Trophy Points:
    420
    #5
    If you have PHPMyAdmin then just run the query in the search tab. After you run it you will see the results and it gives you the query it used. Just use the query in your code and it should work.
     
    aaron_nimocks, Mar 1, 2008 IP
  6. centralexpert

    centralexpert Peon

    Messages:
    27
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Ok i'm a little short on experience with PHP/MySQL but i'll give it a try.

    Thanks
     
    centralexpert, Mar 1, 2008 IP
  7. aaron_nimocks

    aaron_nimocks Im kind of a big deal Staff

    Messages:
    5,563
    Likes Received:
    627
    Best Answers:
    0
    Trophy Points:
    420
    #7
    Your code

    $result = mysql_query("SELECT * FROM downloads WHERE message LIKE '%$search%'");
    PHP:
    is saying..

    Select everything in that "row" from the "table" downloads where the "field" message is like $search aka, whatever they are searching for.

    So you should have a field called message in the downloads table. Now it might not be returning anything because it couldnt find anything also. I would use a search that you know will return something.
     
    aaron_nimocks, Mar 1, 2008 IP
  8. centralexpert

    centralexpert Peon

    Messages:
    27
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Ok so I used search in phpMyAdmin as you said and was given 9 results as i thought.

    I then copy and pasted the query into my existing code but still there was no result.

    I viewed the source code and all thats being displayed is the 5 <br>'s at the bottom of the script.

    Any suggestions?
     
    centralexpert, Mar 1, 2008 IP
  9. centralexpert

    centralexpert Peon

    Messages:
    27
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    I don't understand why it's not displaying any results.

    I'm have a small script that displays the last 5 entries to the db 'downloads' in a simple html table on my home page.
    In this table are the words i'm using in my search.

    What does this indicate to you?
     
    centralexpert, Mar 1, 2008 IP
  10. elias_sorensen

    elias_sorensen Well-Known Member

    Messages:
    852
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    110
    #10
    Try to count the result, to see if you get some info...
    $countQuery = mysql_num_rows($result);
    echo $countQuery;

    if not.. try go echo your search post:
    echo $search;

    you could also try to echo your whole $row array:
    print_r($row);

    Good luck :)
     
    elias_sorensen, Mar 2, 2008 IP