-1

Possible Duplicate:
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in

    function arr($string)
    {
        $tank5 = "SELECT url FROM `db`.`tank` WHERE url LIKE {$string}";
        $result5 = mysql_query($tank5);
            if (!$result5) {
    die('Invalid query: ' . mysql_error());
}

        $parts2 = array();
        while ($p = mysql_fetch_array($result5)) {
            $parts2[] = $p['url'];
        }

        // return the array created.
        return $parts2; 
    }

    $array5 = arr('lol.com');       
    print_r($array5);

Why don't the above code work. The error I'm getting is
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in /usr/local/...
so I guess it's not database connection problem. Any idea?

Community
  • 1
  • 1
sg552
  • 1,459
  • 4
  • 29
  • 55
  • @minitech: Hello, the problem has been solved. The query need percentage sign to lookup similar domain. I update the code for connection checking anyway. – sg552 Feb 25 '12 at 17:25

2 Answers2

3

Most likely, you're using a LIKE clause but not passing wildcards with $string. Therefore, it would only find exact matches.

Instead surround $string with % and single-quotes, since you did not quote the input value (assuming the function's input parameter doesn't already have quotes, which seems unlikely).

$tank5 = "SELECT url FROM `db`.`tank` WHERE url LIKE '%$string%'";

We assume that prior to calling this function, you have already escaped $string against SQL injection like:

$string = 'lol.com';
$string = mysql_real_escape_string($string);
$arr = arr($string);
Michael Berkowski
  • 260,803
  • 45
  • 432
  • 377
  • Thank you very much. That's the problem, percentage sign. I know the query need the percentage sign because I'm busy with this thing since yesterday. I forgot about that thing. Thanks again. – sg552 Feb 25 '12 at 17:32
2

You are not doing any error checking in your query, so it's no wonder your script breaks the query fails. How to do proper error checking is outlined in the manual or in this reference question.

Example:

$result5 = mysql_query($tank5);
if (!$result5)
 { trigger_error("mySQL error: ".mysql_error());  // output the error
   die();
 }

in your specific case, the problem is probably that you are not wrapping your search string in quotes like so:

 $tank5 = "SELECT url FROM `db`.`tank` WHERE url LIKE '{$string}'";
Community
  • 1
  • 1
Pekka
  • 431,103
  • 135
  • 960
  • 1,075
  • Thanks for your advice. I thought if I use the apostrophe outside of function like this `$array5 = arr('lol.com');` it will work. I appreciate your help. – sg552 Feb 25 '12 at 17:29