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 isWarning: 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?
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);
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}'";