SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Thread: Array Challenge

  1. #1
    Non-Member MrLister's Avatar
    Join Date
    Feb 2001
    Posts
    108
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Array Challenge

    Lets see if anybody can figure this error out.
    error:Warning: Supplied argument is not a valid MySQL result resource in c:\program files\apache group\apache\htdocs\forum3\cheats\pc\admin\search.php on line 15


    $searchString = "changed ad";
    $searchWords = array();
    $searchWords = explode(" ", $searchString)
    while(list($var, $value)=each($searchWords)) {
    $searchWords[$var] = "title LIKE '%" . trim($value) ." %'";
    }
    if(sizeof($searchWords) > 0) {
    $whereClause = implode(" OR ", $searchWords);
    }

    $result = mysql_query("SELECT essay_name FROM essays_pc WHERE essay_name like '$whereClause'");
    while(list($essay_name) = mysql_fetch_row($result)){
    echo("$essay_name<br>");
    }

    Line 15 is while(list($essay_name) = mysql_fetch_row($result)){

  2. #2
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have recently taken to writing my queries like so:
    PHP Code:
    $sql "SELECT essay_name 
            FROM essays_pc 
            WHERE essay_name like '
    $whereClause'";
            
    $result mysql_query($sql) or die ("<br>Database error X <br>$sql<br>");
    ... 
    The X in "Database error X" is just a sequential number I give to each mysql_query so that if the query fails I not only have the sql query used dispayed in the browser for debugging purposes, but I can quickly find the relevent query in my source code.

    I suggest you do yourself a favour and print out the sql statements for debugging purposes. They make your syntax errors much more obvious. Also, you can then cut an paste the actual sql into the mysql client or phpMyAdmin and test it there as well.

    That said, I can see one problem with the sql immediately:
    "SELECT essay_name FROM essays_pc WHERE essay_name like '$whereClause'"

    Lets say that there is one search word "foo".
    $searchWords[0] = "title LIKE '%foo%'";

    So the SQL ends up looking like:
    SELECT essay_name
    FROM essays_pc
    WHERE essay_name
    like 'title LIKE '%foo%''


    I think . Its easier if you can print out the actual sql into the browser for debugging


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
  •